Skip to content

Commit e932a43

Browse files
authored
[RUA-62] Handle login redirect for non-admin access (#59)
* [RUA-62] handle non-admin redirect * [RUA-62] conflict * [RUA-62] move to new architecture
1 parent bb11cf7 commit e932a43

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/Module/AdminAccess.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ class AdminAccess implements HookSubscriberInterface
1616
{
1717
public function subscribe(HookService $service)
1818
{
19-
if (!is_admin()) {
20-
return;
19+
if (is_admin()) {
20+
$service->add_action('auth_redirect', [$this, 'authorize_admin_access']);
2121
}
2222

23-
$service->add_action('auth_redirect', [$this, 'authorize_admin_access']);
23+
$service->add_filter( 'login_redirect',
24+
[$this, 'admin_login_redirect'],
25+
10,
26+
3
27+
);
2428
}
2529

2630
/**
@@ -57,4 +61,31 @@ public function authorize_admin_access($user_id)
5761

5862
wp_die(__('Sorry, you are not allowed to access this page.'));
5963
}
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+
}
6091
}

0 commit comments

Comments
 (0)