-
Notifications
You must be signed in to change notification settings - Fork 1
/
apps_entity_restrictions.module
420 lines (367 loc) · 11.2 KB
/
apps_entity_restrictions.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/**
* @file
* Main module file for the Apps entity restrictions module.
*/
/**
* Implements hook_entity_info().
*/
function apps_entity_restrictions_entity_info() {
return array(
'apps_entity_restrictions' => array(
'label' => t('Apps entity restrictions'),
'entity class' => 'AppsEntityRestriction',
'controller class' => 'AppsEntityRestrictionsControllerExportable',
'base table' => 'apps_entity_restrictions',
'entity keys' => array(
'id' => 'id',
'label' => 'title',
'name' => 'app_key',
),
'exportable' => TRUE,
'module' => 'apps_entity_restrictions',
'export' => array(
'default hook' => 'hook_default_apps_entity_restrictions',
),
),
);
}
/**
* Implements hook_apps_entity_restrictions_entity_ignore().
*/
function apps_entity_restrictions_apps_entity_restrictions_entity_ignore() {
return array(
'apps_entity_restrictions',
);
}
/**
* Create apps entity restriction entity.
*
* @param $data
* Initialize data of the Apps entity restrictions.
*
* @return AppsEntityRestriction
* Initialized object a Apps entity restrictions.
*/
function apps_entity_restrictions_create($data = array()) {
global $user;
$data += array(
'uid' => $user->uid,
'time' => REQUEST_TIME,
'status' => !variable_get('apps_need_approve'),
);
return entity_create('apps_entity_restrictions', $data);
}
/**
* Load multiple Apps entity restrictions entity.
*
* @param $ids
* Array of Apps entity restrictions IDs.
*
* @return AppsEntityRestriction[]
* Array of Apps entity restrictions objects.
*/
function apps_entity_restrictions_load_multiple($ids) {
return entity_load('apps_entity_restrictions', $ids);
}
/**
* Load a single Apps entity restrictions entity.
*
* @param $id
* Id a Apps entity restrictions entity.
*
* @return AppsEntityRestriction
* Apps entity restrictions object.
*/
function apps_entity_restrictions_load($id) {
return entity_load_single('apps_entity_restrictions', $id);
}
/**
* Delete multiple Apps entity restrictions entity.
*
* @param $ids
* List of Apps entity restrictions IDs to delete.
*/
function apps_entity_restrictions_delete_multiple($ids) {
entity_delete_multiple('apps_entity_restrictions', $ids);
}
/**
* Implements hook_menu().
*/
function apps_entity_restrictions_menu() {
$items = array();
$items['admin/apps'] = array(
'title' => 'Apps',
'description' => 'Create/edit/delete apps',
'page callback' => array('apps_entity_restrictions_main_app_page'),
'access callback' => 'apps_entity_restrictions_access_main_app_page',
'file' => 'apps_entity_restrictions.misc.inc',
'file path' => drupal_get_path('module', 'apps_entity_restrictions') . '/pages/',
);
$items['admin/apps/add'] = array(
'title' => 'Create app',
'description' => 'Add an app',
'type' => MENU_LOCAL_ACTION,
'access arguments' => array('create app'),
'page callback' => 'drupal_get_form',
'page arguments' => array('apps_entity_restrictions_app_form'),
'file' => 'apps_entity_restrictions.crud.inc',
'file path' => drupal_get_path('module', 'apps_entity_restrictions') . '/pages/',
);
$items['admin/apps/%apps_entity_restrictions/edit'] = array(
'title' => 'Edit',
'description' => 'Edit the app settings',
'access callback' => 'apps_entity_restrictions_access',
'access arguments' => array('edit', 2),
'page callback' => 'drupal_get_form',
'page arguments' => array('apps_entity_restrictions_app_form', 2),
'file' => 'apps_entity_restrictions.crud.inc',
'file path' => drupal_get_path('module', 'apps_entity_restrictions') . '/pages/',
'type' => MENU_LOCAL_TASK,
'weight' => 0,
);
$items['admin/apps/%apps_entity_restrictions/delete'] = array(
'title' => 'Delete',
'description' => 'Delete an app.',
'access callback' => 'apps_entity_restrictions_access',
'access arguments' => array('delete', 2),
'page callback' => 'drupal_get_form',
'page arguments' => array('apps_entity_restrictions_app_delete', 2),
'file' => 'apps_entity_restrictions.crud.inc',
'file path' => drupal_get_path('module', 'apps_entity_restrictions') . '/pages/',
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
if (module_exists('devel')) {
$items['admin/apps/%apps_entity_restrictions/devel'] = array(
'title' => 'Devel',
'page callback' => 'devel_load_object',
'page arguments' => array('apps_entity_restrictions', 2),
'access arguments' => array('access devel information'),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'devel.pages.inc',
'file path' => drupal_get_path('module', 'devel'),
'weight' => 20,
);
}
return $items;
}
/**
* Implements hook_admin_paths().
*/
function apps_entity_restrictions_admin_paths() {
return array(
'admin/apps' => TRUE,
'admin/apps/add' => TRUE,
'admin/apps/apps/*' => TRUE,
'admin/apps/apps/*/edit' => TRUE,
'admin/apps/apps/*/devel' => TRUE,
'admin/apps/apps/*/delete' => TRUE,
);
}
/**
* Access callback - determine if the user has access to the main app page.
*
* @param $account
* The user object.
*
* @return bool
*/
function apps_entity_restrictions_access_main_app_page($account = NULL) {
if (!$account) {
global $user;
$account = user_load($user->uid);
}
$permissions = array(
'manage apps', 'edit app', 'edit own app', 'delete app', 'delete own app',
);
foreach ($permissions as $permission) {
if (!user_access($permission, $account)) {
return FALSE;
}
}
return TRUE;
}
/**
* Access callback for action on app.
*
* @param $operation
* The operation name.
* @param AppsEntityRestriction $app
* The app object
* @param $account
* The user ID.
*
* @return bool
* Return true or false if the user has access for this action.
*/
function apps_entity_restrictions_access($operation, AppsEntityRestriction $app, $account = NULL) {
if ($operation == 'delete') {
return $app->isAppExported() === TRUE;
}
if (!$account) {
global $user;
$account = user_load($user->uid);
}
// Check if the user can manage apps. The manage apps permission are bypass
// any permission.
if (user_access('manage apps')) {
return TRUE;
}
$edit_permission = $operation;
if ($account->uid == $app->uid) {
$edit_permission .= ' own';
}
$edit_permission .= ' app';
// Collect access information from other modules. See first if other modules
// allowed this action. Only TRUE will accepted.
$invoked_access = module_invoke_all('apps_entity_restrictions_access', $operation, $app, $account);
if (in_array(TRUE, $invoked_access, TRUE)) {
return TRUE;
}
return user_access($edit_permission, $account);
}
/**
* Implements hook_permission().
*/
function apps_entity_restrictions_permission() {
return array(
'create app' => array(
'title' => t('Create an app'),
'description' => t('Allow the user a permission to create an app.'),
),
'edit own app' => array(
'title' => t('Edit own app'),
'description' => t('Allow to the user edit his own app.'),
),
'edit app' => array(
'title' => t('Edit app'),
'description' => t('Allow user to edit apps which he not own them.'),
),
'delete own app' => array(
'title' => t('Delete own app'),
'description' => t('Allow to the user delete his app.'),
),
'delete app' => array(
'title' => t('Delete other app'),
'description' => t('Allow to the user delete apps which he not own them.'),
),
'can approve app' => array(
'title' => t('Can approve app'),
'description' => t('When apps need to be approved, the user can approve apps.'),
),
'manage apps' => array(
'title' => t('Manage app'),
'description' => t('Grant to the user manage apps. Give this to trusty users.'),
),
'manage settings' => array(
'title' => t('Manage settings'),
'description' => t('Allow to user manage the settings of Apps entity restrictions. Give this to trusty users.'),
),
);
}
/**
* Load app entity by key and secret.
*
* @param $key
* The key of the app.
* @param $secret
* The secret key of the app. Optional.
*
* @return AppsEntityRestriction
*/
function apps_entity_restrictions_load_by_keys($key, $secret = NULL) {
$info = &drupal_static(__FUNCTION__, array());
$static_identifier = $key . '_' . $secret;
if (!empty($info[$static_identifier])) {
$return = $info[$static_identifier];
}
else {
$query = new entityFieldQuery();
$query
->entityCondition('entity_type', 'apps_entity_restrictions')
->propertyCondition('app_key', $key);
if ($secret) {
$query->propertyCondition('app_secret', $secret);
}
$result = $query->execute();
$info[$static_identifier] = !empty($result['apps_entity_restrictions']) ? apps_entity_restrictions_load(reset($result['apps_entity_restrictions'])->id) : '';
$return = $info[$static_identifier];
}
return $return;
}
/**
* Build a list of properties and fields for an entity.
*
* @param $entity
* The entity name.
*
* @return array
* List of fields and properties.
*/
function apps_entity_restrictions_build_fields($entity) {
$schema_info = array();
// Get the schema fields.
$info = entity_get_info($entity);
foreach ($info['schema_fields_sql']['base table'] as $property) {
$schema_info[$property] = ucfirst(str_replace('_', ' ', $property));
}
// Get the fields names.
$field_instances = field_info_instances($entity);
foreach ($field_instances as $bundle => $fields) {
foreach ($fields as $name => $field) {
$content_type_info = node_type_get_type($bundle);
$schema_info[$name] = $field['label'];
$schema_info[$name] .= $content_type_info ? " (" . $content_type_info->name . ")" : '';
}
}
return $schema_info;
}
/**
* Retrieve a list of entities which meant to be ignored in order for apps to
* not display their content.
*/
function apps_entity_restrictions_ignored_apps() {
if ($ignore = cache_get('apps_entity_restrictions_ignore')) {
// Information already cached - use him.
return $ignore->data;
}
// Get all the entities which users are not allowed to access their entries.
$ignore = module_invoke_all('apps_entity_restrictions_entity_ignore');
// Let other module to alter the list of entities.
drupal_alter('apps_entity_restrictions_entity_ignore', $ignore);
// Cache the ignored apps.
cache_set('apps_entity_restrictions_ignore', $ignore);
return $ignore;
}
/**
* Check if an entity need to be shown as selected.
*
* @param $info_need
* The method and selected properties.
*
* @return bool
*/
function apps_entity_restrictions_is_selected($info_need) {
$methods = $info_need['methods'];
$properties = $info_need['properties'];
$entity_selected = false;
if (!empty($methods)) {
foreach ($methods as $method) {
if ($method) {
$entity_selected = TRUE;
break;
}
}
}
if (!empty($property)) {
foreach ($properties as $property) {
if ($property) {
$entity_selected = TRUE;
break;
}
}
}
return $entity_selected;
}