Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 243 lines (213 sloc) 6.998 kb
e699f7ad »
2012-02-02 Initial commit of roles plugin
1 <?php
2
3 /**
4 * Roles plugin
5 *
6 * @package Roles
7 * @author Andras Szepeshazi
8 * @copyright Arck Interactive, LLC 2011
9 * @link http://www.arckinteractive.com/
10 */
11
26e9eb6a »
2012-02-09 Updates to roles - fixed recursive role extension, added default role…
12 define('DEFAULT_ROLE', '_default_');
13
e699f7ad »
2012-02-02 Initial commit of roles plugin
14 elgg_register_event_handler('init', 'system', 'roles_init');
15
16 function roles_init() {
17
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
18 elgg_register_library('roles', elgg_get_plugins_path().'roles/lib/roles.php');
19 elgg_register_library('roles_config', elgg_get_plugins_path().'roles/lib/config.php');
e699f7ad »
2012-02-02 Initial commit of roles plugin
20
21 elgg_load_library('roles');
22 elgg_load_library('roles_config');
23
26e9eb6a »
2012-02-09 Updates to roles - fixed recursive role extension, added default role…
24 roles_check_update();
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
25
c8500f6a »
2012-02-02 Updates to roles plugin
26 elgg_register_plugin_hook_handler('action', 'all', 'roles_actions_permissions');
49c7d582 »
2012-02-08 Updates to roles plugin - implemented role based page handling
27 elgg_register_plugin_hook_handler('route', 'all', 'roles_pages_permissions');
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
28
c8500f6a »
2012-02-02 Updates to roles plugin
29 elgg_register_event_handler('pagesetup', 'system', 'roles_menus_permissions');
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
30 elgg_register_event_handler('ready', 'system', 'roles_hooks_permissions');
e699f7ad »
2012-02-02 Initial commit of roles plugin
31
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
32 roles_register_views_hook_handler();
e699f7ad »
2012-02-02 Initial commit of roles plugin
33
34 }
35
0abcb4c6 »
2012-02-08 Updates to roles (implemented role based view handling)
36 function roles_register_views_hook_handler() {
37 $role = roles_get_role();
38 if (elgg_instanceof($role, 'object', 'role')) {
39 $role_perms = roles_get_role_permissions($role, 'views');
40 if (is_array($role_perms) && !empty($role_perms)) {
41 foreach ($role_perms as $view => $perm_details) {
42 switch ($perm_details['rule']) {
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
43 case 'deny':
44 elgg_register_plugin_hook_handler('view', $view, 'roles_views_permissions');
45 break;
46 case 'extend':
47 $params = $perm_details['view_extension'];
48 $view_extension = $params['view'];
49 $priority = isset($params['priority']) ? $params['priority'] : 501;
50 $viewtype = isset($params['viewtype']) ? $params['viewtype'] : '';
51 elgg_extend_view($view, $view_extension, $priority, $viewtype);
52 break;
53 case 'replace':
54 $params = $perm_details['view_replacement'];
55 $location = elgg_get_root_path().$params['location'];
56 $viewtype = isset($params['viewtype']) ? $params['viewtype'] : '';
57 elgg_set_view_location($view, $location, $viewtype);
58 break;
59 case 'allow':
60 default:
61 break;
0abcb4c6 »
2012-02-08 Updates to roles (implemented role based view handling)
62 }
63 }
64 }
65 }
66 }
67
c8500f6a »
2012-02-02 Updates to roles plugin
68 function roles_views_permissions($hook_name, $entity_type, $return_value, $params) {
0abcb4c6 »
2012-02-08 Updates to roles (implemented role based view handling)
69
70 $role = roles_get_role();
71 if (elgg_instanceof($role, 'object', 'role')) {
72 $role_perms = roles_get_role_permissions($role, 'views');
73 if (is_array($role_perms) && !empty($role_perms)) {
74 foreach ($role_perms as $view => $perm_details) {
75 if ($params['view'] == $view) {
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
76 return ''; // Supress view output
77 }
0abcb4c6 »
2012-02-08 Updates to roles (implemented role based view handling)
78 }
79 }
49c7d582 »
2012-02-08 Updates to roles plugin - implemented role based page handling
80 }
e699f7ad »
2012-02-02 Initial commit of roles plugin
81 }
82
c1b15e50 »
2012-02-09 Updates to roles plugin - role base action handling
83 function roles_actions_permissions($hook, $type, $return_value, $params) {
26e9eb6a »
2012-02-09 Updates to roles - fixed recursive role extension, added default role…
84 $role = roles_get_role();
85 if (elgg_instanceof($role, 'object', 'role')) {
86 $role_perms = roles_get_role_permissions($role, 'actions');
87 if (is_array($role_perms) && !empty($role_perms)) {
88 foreach ($role_perms as $action => $perm_details) {
c1b15e50 »
2012-02-09 Updates to roles plugin - role base action handling
89 if ($action == $type) {
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
90 switch ($perm_details['rule']) {
91 case 'deny':
92 register_error(elgg_echo('roles:action:denied'));
93 return false;
94 break;
95 case 'allow':
96 default:
97 break;
c1b15e50 »
2012-02-09 Updates to roles plugin - role base action handling
98 }
99 }
26e9eb6a »
2012-02-09 Updates to roles - fixed recursive role extension, added default role…
100 }
101 }
102 }
e699f7ad »
2012-02-02 Initial commit of roles plugin
103 }
104
c8500f6a »
2012-02-02 Updates to roles plugin
105 function roles_menus_permissions($event, $type, $object) {
0abcb4c6 »
2012-02-08 Updates to roles (implemented role based view handling)
106
c8500f6a »
2012-02-02 Updates to roles plugin
107 $role = roles_get_role();
108 if (elgg_instanceof($role, 'object', 'role')) {
109 $role_perms = roles_get_role_permissions($role, 'menus');
110 if (is_array($role_perms) && !empty($role_perms)) {
6e3f1f57 »
2012-02-08 Updates to roles plugin (finalized role based menu extensions)
111 foreach ($role_perms as $menu => $perm_details) {
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
112 switch ($perm_details['rule']) {
113 case 'deny':
114 list($menu_name, $item_name) = explode('::', $menu);
115 elgg_unregister_menu_item($menu_name, $item_name);
116 break;
117 case 'extend':
118 $menu_item = roles_prepare_menu_vars($perm_details['menu_item']);
119 $menu_obj = ElggMenuItem::factory($menu_item);
120 elgg_register_menu_item($menu, $menu_obj);
121 break;
122 case 'replace':
123 list($menu_name, $item_name) = explode('::', $menu);
124 $menu_item = roles_prepare_menu_vars($perm_details['menu_item']);
125 $menu_obj = ElggMenuItem::factory($menu_item);
126 roles_replace_menu($menu_name, $item_name, $menu_obj);
127 break;
128 case 'allow':
129 default:
130 break;
c8500f6a »
2012-02-02 Updates to roles plugin
131 }
132 }
133 }
134 }
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
135
c8500f6a »
2012-02-02 Updates to roles plugin
136 return true;
e699f7ad »
2012-02-02 Initial commit of roles plugin
137 }
138
49c7d582 »
2012-02-08 Updates to roles plugin - implemented role based page handling
139 function roles_pages_permissions($hook_name, $entity_type, $return_value, $params) {
140 $role = roles_get_role();
141 if (elgg_instanceof($role, 'object', 'role')) {
142 $role_perms = roles_get_role_permissions($role, 'pages');
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
143 $page_path = $return_value['handler'].'/'.implode('/', $return_value['segments']);
49c7d582 »
2012-02-08 Updates to roles plugin - implemented role based page handling
144 if (is_array($role_perms) && !empty($role_perms)) {
145 foreach ($role_perms as $page => $perm_details) {
146 if (roles_replace_dynamic_paths($page) == $page_path) {
147 switch ($perm_details['rule']) {
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
148 case 'deny':
149 register_error(elgg_echo('roles:page:denied'));
150 if (isset($perm_details['forward'])) {
151 forward($perm_details['forward']);
152 } else {
153 forward(REFERER);
154 }
155 break;
156 case 'redirect':
157 if (isset($perm_details['forward'])) {
158 forward($perm_details['forward']);
159 } else {
160 forward(REFERER);
161 }
162 break;
163 case 'allow':
164 default:
165 break;
49c7d582 »
2012-02-08 Updates to roles plugin - implemented role based page handling
166 }
167 }
168 }
169 }
170 }
e699f7ad »
2012-02-02 Initial commit of roles plugin
171 }
172
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
173 function roles_hooks_permissions($event, $type, $object) {
174
175 $role = roles_get_role();
176 if (elgg_instanceof($role, 'object', 'role')) {
177 $role_perms = roles_get_role_permissions($role, 'hooks');
178 if (is_array($role_perms) && !empty($role_perms)) {
179 foreach ($role_perms as $hook => $perm_details) {
180 list($hook_name, $type) = explode('::', $hook);
181 if (!$type) {
182 $type = 'all';
183 }
184 switch ($perm_details['rule']) {
185 case 'deny':
186 $params = $perm_details['hook'];
187 if (is_array($params)) {
188 $handler = $params['handler'];
189 elgg_unregister_plugin_hook_handler($hook_name, $type, $handler);
190 } else {
191 global $CONFIG;
192 unset($CONFIG->hooks[$hook_name][$type]);
193 }
194 break;
195 case 'extend':
196 $params = $perm_details['hook'];
197 $handler = $params['handler'];
198 $priority = isset($params['priority']) ? $params['priority'] : 500;
199 elgg_register_plugin_hook_handler($hook_name, $type, $handler, $priority);
200 break;
201 case 'replace':
202 $params = $perm_details['hook'];
203 $old_handler = $params['old_handler'];
204 $new_handler = $params['new_handler'];
205 $priority = isset($params['priority']) ? $params['priority'] : 500;
206 elgg_unregister_plugin_hook_handler($hook_name, $type, $old_handler);
207 elgg_register_plugin_hook_handler($hook_name, $type, $new_handler, $priority);
208 break;
209 case 'allow':
210 default:
211 break;
212 }
213 }
214 }
215 }
216
217 return true;
218 }
219
220 function roles_user_settings_save($hook_name, $entity_type, $return_value, $params) {
e699f7ad »
2012-02-02 Initial commit of roles plugin
221 $role_name = get_input('role');
222 $user_id = get_input('guid');
26e9eb6a »
2012-02-09 Updates to roles - fixed recursive role extension, added default role…
223
c8500f6a »
2012-02-02 Updates to roles plugin
224 $error = false;
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
225
26e9eb6a »
2012-02-09 Updates to roles - fixed recursive role extension, added default role…
226 $role = roles_get_role_by_name($role_name);
227 $user = get_entity($user_id);
228
229 $res = roles_set_role($role, $user);
230
231 if ($res === false) {
e699f7ad »
2012-02-02 Initial commit of roles plugin
232 register_error(elgg_echo('user:role:fail'));
c8500f6a »
2012-02-02 Updates to roles plugin
233 return false;
26e9eb6a »
2012-02-09 Updates to roles - fixed recursive role extension, added default role…
234 } else if ($res === true) {
c8500f6a »
2012-02-02 Updates to roles plugin
235 system_message(elgg_echo('user:role:success'));
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
236 return true;
c8500f6a »
2012-02-02 Updates to roles plugin
237 }
238
239 return null;
cad9ef4b »
2012-02-09 Updates to roles plugin - implemented role based hook overrides
240
e699f7ad »
2012-02-02 Initial commit of roles plugin
241 }
242
243 ?>
Something went wrong with that request. Please try again.