-
Notifications
You must be signed in to change notification settings - Fork 5
/
entity_boilerplate.module
524 lines (470 loc) · 19.3 KB
/
entity_boilerplate.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<?php
/**
* @file
* Provides basic underlying functionality and configuration options used
* by entity_boilerplate.
*/
module_load_include('inc', 'entity_boilerplate', 'my_entity.api');
/**
* Implements hook_entity_info().
*/
function entity_boilerplate_entity_info() {
$info = array();
$info['my_entity'] = array(
'label' => t('My Entity'),
'entity class' => '\Drupal\entity_boilerplate\Entity\MyEntity\MyEntity',
'controller class' => '\Drupal\entity_boilerplate\Entity\MyEntity\MyEntityController',
'base table' => 'my_entity',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'eid',
'label' => 'title',
'bundle' => 'type',
),
'bundle keys' => array(
'bundle' => 'type',
),
'access callback' => 'my_entity_access',
'access arguments' => array(
'user key' => 'uid',
'access tag' => 'my_entity_access',
),
'permission labels' => array(
'singular' => t('My Entity'),
'plural' => t('My Entities'),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'entity_boilerplate',
'views controller class' => '\Drupal\entity_boilerplate\Entity\MyEntity\MyEntityViewsController',
'admin ui' => array(
'path' => 'admin/content/my_entity',
'file' => 'my_entity.entity.inc',
'controller class' => '\Drupal\entity_boilerplate\Entity\MyEntity\MyEntityUIController',
'menu wildcard' => '%my_entity'
),
);
// Add bundle info but bypass entity_load() as we cannot use it here.
$types = db_select('my_entity_type', 't')
->fields('t')
->execute()
->fetchAllAssoc('type');
foreach ($types as $type_name => $type) {
$info['my_entity']['bundles'][$type_name] = array(
'label' => $type->label,
'admin' => array(
'path' => 'admin/structure/my_entity-types/manage/%my_entity_type',
'real path' => 'admin/structure/my_entity-types/manage/' . $type_name,
'bundle argument' => 4,
),
);
}
$info['my_entity_type'] = array(
'label' => t('My Entity Type'),
'entity class' => '\Drupal\entity_boilerplate\Entity\MyEntity\MyEntityType',
'controller class' => '\Drupal\entity_boilerplate\Entity\MyEntity\MyEntityTypeController',
'base table' => 'my_entity_type',
'fieldable' => FALSE,
'bundle of' => 'my_entity',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
'access callback' => 'my_entity_type_access',
'module' => 'entity_boilerplate',
'admin ui' => array(
'path' => 'admin/structure/my_entity-types',
'file' => 'my_entity.entity.inc',
'controller class' => '\Drupal\entity_boilerplate\Entity\MyEntity\MyEntityTypeUIController',
),
);
return $info;
}
/**
* Implements hook_entity_property_info_alter().
*/
function entity_boilerplate_entity_property_info_alter(&$info) {
$properties = &$info['my_entity']['properties'];
$properties['created'] = array(
'label' => t('Date created'),
'type' => 'date',
'description' => t('The date the My Entity was posted.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer nodes',
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t('Date changed'),
'type' => 'date',
'schema field' => 'changed',
'description' => t('The date the My Entity was most recently updated.'),
);
$properties['uid'] = array(
'label' => t('Author'),
'type' => 'user',
'description' => t('The author of the My Entity.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer my_entity entities',
'required' => TRUE,
'schema field' => 'uid',
);
}
/**
* Implements hook_forms().
*/
function entity_boilerplate_forms() {
$forms = array();
if ($types = my_entity_types()) {
foreach (array_keys($types) as $type) {
$forms[$type . '_my_entity_form']['callback'] = 'my_entity_form';
}
}
return $forms;
}
/**
* Access callback: determines if the user can create any type of My Entity.
*/
function entity_boilerplate_my_entity_add_any_access() {
// Grant automatic access to users with administer my_entity permission.
if (user_access('administer my_entity entities')) {
return TRUE;
}
// Check the user's access on a product type basis.
foreach (my_entity_types() as $type => $member_type) {
if (my_entity_access('create', entity_create('my_entity', array('type' => $type)))) {
return TRUE;
}
}
return FALSE;
}
/**
* Implements hook_permission().
*/
function entity_boilerplate_permission() {
$permissions = array(
'administer my_entity_type entities' => array(
'title' => t('Administer My Entity types'),
'description' => t('Allows users to add My Entity types and configure their fields.'),
'restrict access' => TRUE,
),
);
$permissions += entity_boilerplate_entity_access_permissions('my_entity');
return $permissions;
}
/**
* Implements hook_query_TAG_alter().
*/
function entity_boilerplate_query_my_entity_access_alter(QueryAlterableInterface $query) {
return entity_boilerplate_entity_access_query_alter($query, 'my_entity');
}
/**
* Generic access control for My Entity entities.
*
* @param $op
* The operation being performed. One of 'view', 'update', 'create' or
* 'delete'.
* @param $entity
* Optionally an entity to check access for. If no entity is given, it will be
* determined whether access is allowed for all entities of the given type.
* @param $account
* The user to check for. Leave it to NULL to check for the global user.
* @param $entity_type
* The entity type of the entity to check for.
*
* @see entity_access()
*
* @return bool
* Whether the user has access to perform the operation or not.
*/
function entity_boilerplate_entity_access($op, $entity, $account = NULL, $entity_type) {
global $user;
$account = isset($account) ? $account : $user;
$entity_info = entity_get_info($entity_type);
if ($op == 'view') {
if (isset($entity)) {
// When trying to figure out access to an entity, query the base table using
// our access control tag.
if (!empty($entity_info['access arguments']['access tag']) && module_implements('query_' . $entity_info['access arguments']['access tag'] . '_alter')) {
$query = db_select($entity_info['base table']);
$query->addExpression('1');
return (bool) $query
->addTag($entity_info['access arguments']['access tag'])
->addMetaData('account', $account)
->condition($entity_info['entity keys']['id'], $entity->{$entity_info['entity keys']['id']})
->range(0, 1)
->execute()
->fetchField();
}
else {
return TRUE;
}
}
else {
return user_access('view any ' . $entity_type . ' entity', $account);
}
}
else {
// First grant access to the entity for the specified operation if no other
// module denies it and at least one other module says to grant access.
$access_results = module_invoke_all('my_entity_entity_access', $op, $entity, $account, $entity_type);
if (in_array(FALSE, $access_results, TRUE)) {
return FALSE;
}
elseif (in_array(TRUE, $access_results, TRUE)) {
return TRUE;
}
// Grant generic administrator level access.
if (user_access('administer ' . $entity_type . ' entities', $account)) {
return TRUE;
}
// Grant access based on entity type and bundle specific permissions with
// special handling for the create operation since the entity passed in will
// be initialized without ownership.
if ($op == 'create') {
// Assuming an entity was passed in and we know its bundle key, perform
// the entity type and bundle-level access checks.
if (isset($entity) && !empty($entity_info['entity keys']['bundle'])) {
return user_access('create ' . $entity_type . ' entities', $account) || user_access('create ' . $entity_type . ' entities of bundle ' . $entity->{$entity_info['entity keys']['bundle']}, $account);
}
else {
// Otherwise perform an entity type-level access check.
return user_access('create ' . $entity_type . ' entities', $account);
}
}
else {
// Next perform checks for the edit and delete operations. Begin by
// extracting the bundle name from the entity if available.
$bundle_name = '';
if (isset($entity) && !empty($entity_info['entity keys']['bundle'])) {
$bundle_name = $entity->{$entity_info['entity keys']['bundle']};
}
// For the edit and delete operations, first perform the entity type and
// bundle-level access check for any entity.
if (user_access('edit any ' . $entity_type . ' entity', $account) ||
user_access('edit any ' . $entity_type . ' entity of bundle ' . $bundle_name, $account)) {
return TRUE;
}
// Then check an authenticated user's access to edit his own entities.
if ($account->uid && !empty($entity_info['access arguments']['user key']) && isset($entity->{$entity_info['access arguments']['user key']}) && $entity->{$entity_info['access arguments']['user key']} == $account->uid) {
if (user_access('edit own ' . $entity_type . ' entities', $account) ||
user_access('edit own ' . $entity_type . ' entities of bundle ' . $bundle_name, $account)) {
return TRUE;
}
}
}
}
return FALSE;
}
/**
* Return permission names for a given entity type.
*/
function entity_boilerplate_entity_access_permissions($entity_type) {
$entity_info = entity_get_info($entity_type);
$labels = $entity_info['permission labels'];
$permissions = array();
// General 'administer' permission.
$permissions['administer ' . $entity_type . ' entities'] = array(
'title' => t('Administer @entity_type', array('@entity_type' => $labels['plural'])),
'description' => t('Allows users to perform any action on @entity_type.', array('@entity_type' => $labels['plural'])),
'restrict access' => TRUE,
);
// Generic create and edit permissions.
$permissions['create ' . $entity_type . ' entities'] = array(
'title' => t('Create @entity_type of any type', array('@entity_type' => $labels['plural'])),
);
if (!empty($entity_info['access arguments']['user key'])) {
$permissions['edit own ' . $entity_type . ' entities'] = array(
'title' => t('Edit own @entity_type of any type', array('@entity_type' => $labels['plural'])),
);
}
$permissions['edit any ' . $entity_type . ' entity'] = array(
'title' => t('Edit any @entity_type of any type', array('@entity_type' => $labels['singular'])),
'restrict access' => TRUE,
);
if (!empty($entity_info['access arguments']['user key'])) {
$permissions['view own ' . $entity_type . ' entities'] = array(
'title' => t('View own @entity_type of any type', array('@entity_type' => $labels['plural'])),
);
}
$permissions['view any ' . $entity_type . ' entity'] = array(
'title' => t('View any @entity_type of any type', array('@entity_type' => $labels['singular'])),
'restrict access' => TRUE,
);
// Per-bundle create and edit permissions.
if (!empty($entity_info['entity keys']['bundle'])) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
$permissions['create ' . $entity_type . ' entities of bundle ' . $bundle_name] = array(
'title' => t('Create %bundle @entity_type', array('@entity_type' => $labels['plural'], '%bundle' => $bundle_info['label'])),
);
if (!empty($entity_info['access arguments']['user key'])) {
$permissions['edit own ' . $entity_type . ' entities of bundle ' . $bundle_name] = array(
'title' => t('Edit own %bundle @entity_type', array('@entity_type' => $labels['plural'], '%bundle' => $bundle_info['label'])),
);
}
$permissions['edit any ' . $entity_type . ' entity of bundle ' . $bundle_name] = array(
'title' => t('Edit any %bundle @entity_type', array('@entity_type' => $labels['singular'], '%bundle' => $bundle_info['label'])),
'restrict access' => TRUE,
);
if (!empty($entity_info['access arguments']['user key'])) {
$permissions['view own ' . $entity_type . ' entities of bundle ' . $bundle_name] = array(
'title' => t('View own %bundle @entity_type', array('@entity_type' => $labels['plural'], '%bundle' => $bundle_info['label'])),
);
}
$permissions['view any ' . $entity_type . ' entity of bundle ' . $bundle_name] = array(
'title' => t('View any %bundle @entity_type', array('@entity_type' => $labels['singular'], '%bundle' => $bundle_info['label'])),
'restrict access' => TRUE,
);
}
}
return $permissions;
}
/**
* Generic implementation of hook_query_alter() for My Entity entities.
*/
function entity_boilerplate_entity_access_query_alter($query, $entity_type, $base_table = NULL, $account = NULL) {
global $user;
// Read the account from the query if available or default to the current user.
if (!isset($account) && !$account = $query->getMetaData('account')) {
$account = $user;
}
// Do not apply any conditions for users with administrative view permissions.
if (user_access('administer ' . $entity_type . ' entities', $account)
|| user_access('view any ' . $entity_type . ' entity', $account)) {
return;
}
// Get the entity type info array for the current access check and prepare a
// conditions object.
$entity_info = entity_get_info($entity_type);
// If a base table wasn't specified, attempt to read it from the query if
// available, look for a table in the query's tables array that matches the
// base table of the given entity type, or just default to the first table.
if (!isset($base_table) && !$base_table = $query->getMetaData('base_table')) {
// Initialize the base table to the first table in the array. If a table can
// not be found that matches the entity type's base table, this will result
// in an invalid query if the first table is not the table we expect,
// forcing the caller to actually properly pass a base table in that case.
$tables = $query->getTables();
reset($tables);
$base_table = key($tables);
foreach ($tables as $table_info) {
if (!($table_info instanceof SelectQueryInterface)) {
// If this table matches the entity type's base table, use its table
// alias as the base table for the purposes of bundle and ownership
// access checks.
if ($table_info['table'] == $entity_info['base table']) {
$base_table = $table_info['alias'];
}
}
}
}
// Prepare an OR container for conditions. Conditions will be added that seek
// to grant access, meaning any particular type of permission check may grant
// access even if none of the others apply. At the end of this function, if no
// conditions have been added to the array, a condition will be added that
// always returns FALSE (1 = 0).
$conditions = db_or();
// Perform bundle specific permission checks for the specified entity type.
// In the event that the user has permission to view every bundle of the given
// entity type, $really_restricted will remain FALSE, indicating that it is
// safe to exit this function without applying any additional conditions. If
// the user only had such permission for a subset of the defined bundles,
// conditions representing those access checks would still be added.
$really_restricted = FALSE;
// Loop over every possible bundle for the given entity type.
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
// If the user has access to view entities of the current bundle...
if (user_access('view any ' . $entity_type . ' entity of bundle ' . $bundle_name, $account)) {
// Add a condition granting access if the entity specified by the view
// query is of the same bundle.
$conditions->condition($base_table . '.' . $entity_info['entity keys']['bundle'], $bundle_name);
}
elseif ($account->uid && !empty($entity_info['access arguments']['user key']) && user_access('view own ' . $entity_type . ' entities of bundle ' . $bundle_name, $account)) {
// Otherwise if an authenticated user has access to view his own entities
// of the current bundle and the given entity type has a user ownership key...
$really_restricted = TRUE;
// Add an AND condition group that grants access if the entity specified
// by the view query matches the same bundle and belongs to the user.
$conditions->condition(db_and()
->condition($base_table . '.' . $entity_info['entity keys']['bundle'], $bundle_name)
->condition($base_table . '.' . $entity_info['access arguments']['user key'], $account->uid)
);
}
else {
$really_restricted = TRUE;
}
}
// No further conditions need to be added to the query if we determined above
// that the user has an administrative view permission for any entity of the
// type and bundles represented by the query.
if (!$really_restricted) {
return;
}
// If the given entity type has a user ownership key...
if (!empty($entity_info['access arguments']['user key'])) {
// Perform 'view own' access control for the entity in the query if the user
// is authenticated.
if ($account->uid && user_access('view own ' . $entity_type . ' entities', $account)) {
$conditions->condition($base_table . '.' . $entity_info['access arguments']['user key'], $account->uid);
}
}
// Prepare an array of condition alter hooks to invoke and an array of context
// data for the current query.
$hooks = array(
'entity_boilerplate_entity_access_condition_' . $entity_type,
'entity_boilerplate_entity_access_condition'
);
$context = array(
'account' => $account,
'entity_type' => $entity_type,
'base_table' => $base_table
);
// Allow other modules to add conditions to the array as necessary.
drupal_alter($hooks, $conditions, $context);
// If we have more than one condition based on the entity access permissions
// and any hook implementations...
if (count($conditions)) {
// Add the conditions to the query.
$query->condition($conditions);
}
else {
// Otherwise, since we don't have any possible conditions to match against,
// we falsify this query. View checks are access grants, not access denials.
$query->where('1 = 0');
}
}
/**
* Implements hook_views_api().
*/
function entity_boilerplate_views_api() {
return array(
'api' => '3.0',
'path' => drupal_get_path('module', 'entity_boilerplate') . '/views',
);
}
/**
* Implements hook_field_extra_fields().
*
* Add the entity properties to the display and manage fields screens.
*/
function entity_boilerplate_field_extra_fields() {
$extra = array();
$entity_type = 'my_entity';
$property_info = entity_get_property_info($entity_type);
foreach ($property_info['bundles'] as $bundle_name => $bundle) {
foreach ($property_info['properties'] as $property_name => $info) {
$extra[$entity_type][$bundle_name]['form'][$property_name] = array(
'label' => $info['label'],
'description' => t('Entity property'),
'weight' => 0,
);
$extra[$entity_type][$bundle_name]['display'][$property_name] = array(
'label' => $info['label'],
'description' => t('Entity property'),
'weight' => 0,
);
}
}
return $extra;
}