Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
RPermissionDataProvider.php
1 <?php
2 /**
3  * Rights permission data provider class file.
4  *
5  * @author Christoffer Niska <cniska@live.com>
6  * @copyright Copyright &copy; 2010 Christoffer Niska
7  * @since 0.9.11
8  */
9 class RPermissionDataProvider extends CDataProvider {
10 
11  /**
12  * @property boolean whether to show the parent type
13  * in the inherited item tooltip.
14  */
15  public $displayParentType = false;
16 
17  private $_authorizer;
18  private $_roles;
19  private $_items;
20  private $_permissions;
21  private $_parents;
22 
23  /**
24  * Constructs the data provider.
25  *
26  * @param string $id the data provider identifier.
27  * @param array $config configuration (name=>value) to be applied as the initial property values of this class.
28  *
29  * @return \RPermissionDataProvider
30  */
31  public function __construct($id, $config = array())
32  {
33  $this->setId($id);
34 
35  foreach ($config as $key => $value) {
36  $this->$key = $value;
37  }
38 
39  $this->init();
40  }
41 
42  /**
43  * Initializes the data provider.
44  *
45  * @return void
46  */
47  public function init()
48  {
49  $this->_authorizer = Rights::getAuthorizer();
50 
51  // Set properties and generate the data
52  $this->setRoles();
53  $this->setItems();
54  $this->setPermissions();
55  $this->setParents();
56  $this->generateData();
57  }
58 
59  /**
60  * Sets the roles property.
61  *
62  * @return void
63  */
64  protected function setRoles()
65  {
66  $this->_roles = $this->_authorizer->getRoles(false);
67  }
68 
69  /**
70  * gets roles
71  *
72  * @return array the roles.
73  */
74  public function getRoles()
75  {
76  return $this->_roles;
77  }
78 
79  /**
80  * Sets the items property.
81  *
82  * @return void
83  */
84  protected function setItems()
85  {
86  $type = array(CAuthItem::TYPE_OPERATION, CAuthItem::TYPE_TASK);
87  $this->_items = $this->_authorizer->getAuthItems($type);
88  }
89 
90  /**
91  * Sets the permissions property.
92  *
93  * @return void
94  */
95  protected function setPermissions()
96  {
97  $allPermissions = $this->_authorizer->getPermissions();
98 
99  $permissions = array();
100  foreach ($this->_roles as $roleName => $role) {
101  $permissions[$roleName] = array();
102  foreach ($this->_items as $itemName => $item) {
103  $permissions[$roleName][$itemName] = $this->_authorizer->hasPermission($itemName, null, $allPermissions[$roleName]);
104  }
105  }
106 
107  // Set the permission property
108  $this->_permissions = $permissions;
109  }
110 
111  /**
112  * Sets the parents property.
113  *
114  * @return void
115  */
116  protected function setParents()
117  {
118  $parents = array();
119  foreach ($this->_permissions as $roleName => $rolePermissions) {
120  foreach ($rolePermissions as $itemName => $permission) {
121  if ($permission===Rights::PERM_INHERITED) {
122  $parents[$roleName][$itemName] = $this->_authorizer->getAuthItemParents($itemName, null, $roleName, true);
123  }
124  }
125  }
126 
127  // Set the parents property
128  $this->_parents = $parents;
129  }
130 
131  /**
132  * Generates the data for the data provider.
133  *
134  * @return void
135  */
136  protected function generateData()
137  {
138  $data = array();
139  $permissions = $this->_permissions;
140  $parents = $this->_parents;
141  foreach ($this->_items as $itemName => $item) {
142  $row = array();
143  $row['description'] = $item->getNameLink();
144 
145  foreach ($this->_roles as $roleName => $role) {
146  // Item is directly assigned to the role
147  if ($permissions[$roleName][$itemName]===Rights::PERM_DIRECT) {
148  $permissionColumn = $item->getRevokePermissionLink($role);
149 
150  // Item is inherited by the role from one of its children
151  } else if ($permissions[$roleName][$itemName]===Rights::PERM_INHERITED && isset($parents[$roleName][$itemName])===true) {
152  $permissionColumn = $item->getInheritedPermissionText($parents[$roleName][$itemName], $this->displayParentType);
153  // Item is not assigned to the role
154  } else {
155  $permissionColumn = $item->getAssignPermissionLink($role);
156  }
157 
158  // Populate role column
159  $row[strtolower($roleName)] = isset($permissionColumn)===true ? $permissionColumn : '';
160  }
161 
162  // Append the row to data
163  $data[] = $row;
164  }
165 
166  $this->setData($data);
167  }
168 
169  /**
170  * Fetches the data from the persistent data storage.
171  *
172  * @return array list of data items
173  */
174  protected function fetchData()
175  {
176  return $this->getData();
177  }
178 
179  /**
180  * Fetches the data item keys from the persistent data storage.
181  *
182  * @return array list of data item keys.
183  */
184  protected function fetchKeys()
185  {
186  $keys = array();
187  foreach ($this->getData() as $key => $value) {
188  $keys[] = $key;
189  }
190 
191  return $keys;
192  }
193 
194  /**
195  * Calculates the total number of data items.
196  *
197  * @return integer the total number of data items.
198  */
199  protected function calculateTotalItemCount()
200  {
201  return count($this->getData());
202  }
203 }