PluginProbe
Restrict User Access – Ultimate Membership & Content Protection / 2.8
Restrict User Access – Ultimate Membership & Content Protection v2.8
trunk 1.3 2.0 2.1.3 2.2.3 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.5 2.6 2.6.1 2.7 2.7.1 2.8 2.8.1
restrict-user-access / src / Module / AdminAccess.php

AdminAccess.php in Restrict User Access – Ultimate Membership & Content Protection 2.8, at src/Module/AdminAccess.php

92 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace RestrictUserAccess\Module;
4
5 use RestrictUserAccess\Hook\HookService;
6 use RestrictUserAccess\Hook\HookSubscriberInterface;
7 use RestrictUserAccess\Repository\SettingRepositoryInterface;
8
9 /**
10 * Class AdminAccess
11 *
12 * @author Joachim Jensen <joachim@dev.institute>
13 * @license https://www.gnu.org/licenses/gpl-3.0.html
14 */
15 class AdminAccess implements HookSubscriberInterface
16 {
17 public function subscribe(HookService $service)
18 {
19 if (is_admin()) {
20 $service->add_action('auth_redirect', [$this, 'authorize_admin_access']);
21 }
22
23 $service->add_filter( 'login_redirect',
24 [$this, 'admin_login_redirect'],
25 10,
26 3
27 );
28 }
29
30 /**
31 * @param int $user_id
32 * @return void
33 */
34 public function authorize_admin_access($user_id)
35 {
36 if (defined('DOING_AJAX') && DOING_AJAX) {
37 return;
38 }
39
40 $rua_user = rua_get_user($user_id);
41 if ($rua_user->has_global_access()) {
42 return;
43 }
44
45 $user_levels = $rua_user->get_level_ids();
46 if (empty($user_levels)) {
47 return;
48 }
49
50 $metadata = \RUA_App::instance()->level_manager->metadata()->get('admin_access');
51 foreach ($user_levels as $level_id) {
52 //bail if user has at least 1 level with admin access
53 if ($metadata->get_data($level_id, true)) {
54 return;
55 }
56 }
57
58 if (apply_filters('rua/auth/admin-access', false, $rua_user)) {
59 return;
60 }
61
62 wp_die(__('Sorry, you are not allowed to access this page.'));
63 }
64
65 public function admin_login_redirect($redirect_to, $requested_redirect_to, $user )
66 {
67 $intercept = empty($redirect_to) || mb_strpos($redirect_to, 'wp-admin') !== false;
68 if (!$intercept) {
69 return $redirect_to;
70 }
71
72 $rua_user = rua_get_user($user);
73 if ($rua_user->has_global_access()) {
74 return $redirect_to;
75 }
76
77 $user_levels = $rua_user->get_level_ids();
78 if (empty($user_levels)) {
79 return $redirect_to;
80 }
81
82 $metadata = \RUA_App::instance()->level_manager->metadata()->get('admin_access');
83 foreach ($user_levels as $level_id) {
84 if ($metadata->get_data($level_id, true)) {
85 return $redirect_to;
86 }
87 }
88
89 return home_url();
90 }
91 }
92