1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file |
4
|
|
|
* Adds CMS defined permissions into the code. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* An extension which controls the 'can*' functions in an object. |
9
|
|
|
*/ |
10
|
|
|
class StandardPermissions extends DataExtension implements PermissionProvider { |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Can the user view this? |
14
|
|
|
*/ |
15
|
|
|
public function canView($member = false) { |
|
|
|
|
16
|
|
|
return Permission::check(get_class($this->owner) . '_view'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Can the user edit this? |
21
|
|
|
*/ |
22
|
|
|
public function canEdit($member = false) { |
23
|
|
|
return Permission::check(get_class($this->owner) . '_edit'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Can the user delete this? |
28
|
|
|
*/ |
29
|
|
|
public function canDelete($member = false) { |
30
|
|
|
return Permission::check(get_class($this->owner) . '_delete'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Can the user create this? |
35
|
|
|
*/ |
36
|
|
|
public function canCreate($member = false) { |
37
|
|
|
return Permission::check(get_class($this->owner) . '_create'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Can the user publish this? |
42
|
|
|
*/ |
43
|
|
|
public function canPublish($member = false) { |
|
|
|
|
44
|
|
|
return Permission::check(get_class($this->owner) . '_publish'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get a complete list of all the permissions this class uses. |
49
|
|
|
*/ |
50
|
|
|
public function providePermissions() { |
51
|
|
|
|
52
|
|
|
// Prepare variables. |
53
|
|
|
$permissions = array(); |
54
|
|
|
|
55
|
|
|
// For each class... |
|
|
|
|
56
|
|
|
foreach ($this->getClassList() as $class) { |
57
|
|
|
|
58
|
|
|
// ...add a few permissions. |
59
|
|
|
foreach (array( |
60
|
|
|
'view', 'edit', 'delete', 'create', 'publish', |
61
|
|
|
) as $name) { |
62
|
|
|
|
63
|
|
|
$permissions[$class . '_' . $name] = $class . '_' . $name; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Done. |
68
|
|
|
return $permissions; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get a list of classes which 'we' extend. |
73
|
|
|
*/ |
74
|
|
|
private function getClassList() { |
75
|
|
|
|
76
|
|
|
// Prepare variables. |
77
|
|
|
$classes = array(); |
78
|
|
|
|
79
|
|
|
// Go through all the classes. |
80
|
|
|
foreach (ClassInfo::subclassesFor('DataObject') as $class => $file) { |
|
|
|
|
81
|
|
|
|
82
|
|
|
// What are the extensions for this class? |
83
|
|
|
$extensions = Config::inst()->get($class, 'extensions'); |
84
|
|
|
if (!is_null($extensions)) { |
85
|
|
|
|
86
|
|
|
// Get a list of classes this horizontally extends this. |
87
|
|
|
if (in_array(get_class($this), $extensions)) { |
88
|
|
|
$classes[] = $class; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Done. |
94
|
|
|
return $classes; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.