CMS  Version 3.9
role_manager.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::using("component");
40 
42 {
43  function RoleManager()
44  {
45 
46  }
47 
53  static function deleteRoleFromString(&$items, $del_role, $field = "")
54  {
55  if(count($items) == 0)
56  return;
57 
58  if(!$field)
59  $field = "role";
60 
61  foreach($items as $item)
62  {
63  if(!$item->$field)
64  continue;
65 
66  $newRoles = array();
67 
68  $roles = explode(",", $item->$field);
69  foreach($roles as $role)
70  {
71  if(!preg_match("/$del_role/", $role))
72  $newRoles[] = $role;
73  }
74 
75  if(count($newRoles) > 0)
76  $newRole = implode(",", $newRoles);
77  else
78  $newRole = "";
79 
80  $item->$field = $newRole;
81  $item->filter = new InclusionFilter($field);
82  $item->save();
83  }
84  }
85 
93  static function addRoleToAccessList($items, $role, $field = "role")
94  {
95  if (count($items) == 0) return;
96  if (is_object($items))
97  {
98  if (!$items instanceof DataItem) return;
99  $items = array( $items );
100  }
101 
102  foreach($items as $item)
103  {
104  if (!$item->hasField($field)) continue;
105 
106  $roles = explode(",", $item->get($field));
107  $found = false;
108  foreach($roles as $r)
109  {
110  if ($r == $role)
111  {
112  $found = true;
113  break;
114  }
115  }
116 
117  if (!$found)
118  {
119  $roles[] = $role;
120  $oldFilter = $item->filter;
121  $item->filter = new InclusionFilter($field);
122  $item->set($field, implode(",", $roles));
123  $item->save();
124  $item->filter = $oldFilter;
125  }
126  }
127  }
128 
136  static function removeRoleFromAccessList($items, $role, $field = "role")
137  {
138  if (count($items) == 0) return;
139  if (is_object($items))
140  {
141  if (!$items instanceof DataItem) return;
142  $items = array( $items );
143  }
144 
145  foreach($items as $item)
146  {
147  if (!$item->hasField($field)) continue;
148 
149  $roles = explode(",", $item->get($field));
150  $newRoles =array();
151  $found = false;
152  foreach($roles as $r)
153  {
154  if ($r == $role)
155  {
156  $found = true;
157  }
158  else
159  {
160  $newRoles[] = $r;
161  }
162  }
163 
164  if ($found)
165  {
166  $oldFilter = $item->filter;
167  $item->filter = new InclusionFilter($field);
168  $item->set($field, implode(",", $newRoles));
169  $item->save();
170  $item->filter = $oldFilter;
171  }
172  }
173  }
174 
176  {
178  }
179 
180  static function upgradeComponent($version)
181  {
182  $mgr = new RoleUpgradeManager();
183  $mgr->upgrade($version);
184  }
185 }?>
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static deleteRoleFromString(&$items, $del_role, $field="")
static upgradeComponent($version)
static addRoleToAccessList($items, $role, $field="role")
Adds the specified role to the access list field in each of the supplied DataItems.
static registerSerializationHandler()
static removeRoleFromAccessList($items, $role, $field="role")
Removes the specified role from the access list field in each of the supplied DataItems.
registerHandler($component, $title, $handler)
Registers a serialization handler for a component.
Provides a simple implementation of a SerializationHandler that can serialize a single DataItem class...
$role
Definition: role_form.inc:41