Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
Role.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
37  class Role extends Item
38  {
39  protected static $roleIdToRoleCache = array();
40 
45  public static function getByName($name)
46  {
47  assert('is_string($name)');
48  assert('$name != ""');
49  $bean = ZurmoRedBean::findOne('role', "name = :name ", array(':name' => $name));
50  assert('$bean === false || $bean instanceof RedBean_OODBBean');
51  if ($bean === false)
52  {
53  throw new NotFoundException();
54  }
55  return self::makeModel($bean);
56  }
57 
58  public function __toString()
59  {
60  assert('$this->name === null || is_string($this->name)');
61  if ($this->name === null)
62  {
63  return Zurmo::t('Core', '(Unnamed)');
64  }
65  return $this->name;
66  }
67 
68  public static function getIdsByUsersMemberOfGroup($groupId)
69  {
70  $groupId = intval($groupId);
71  $searchAttributeData['clauses'] = array(
72  1 => array(
73  'attributeName' => 'users',
74  'relatedModelData' => array(
75  'attributeName' => 'groups',
76  'relatedAttributeName' => 'id',
77  'operatorType' => 'equals',
78  'value' => $groupId,
79  ),
80  ),
81  );
82  $searchAttributeData['structure'] = '1';
83  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter(get_called_class());
84  $where = RedBeanModelDataProvider::makeWhere(get_called_class(), $searchAttributeData, $joinTablesAdapter);
85  return static::getSubsetIds($joinTablesAdapter, null, null, $where);
86  }
87 
88  public static function getFromCacheOrDatabase($roleId)
89  {
90  $roleId = intval($roleId);
91  if (!isset(static::$roleIdToRoleCache[$roleId]))
92  {
93  static::$roleIdToRoleCache[$roleId] = static::getById($roleId);
94  }
95  return static::$roleIdToRoleCache[$roleId];
96  }
97 
98  public static function forgetRoleIdToRoleCache()
99  {
100  static::$roleIdToRoleCache = array();
101  }
102 
103  protected static function translatedAttributeLabels($language)
104  {
105  return array_merge(parent::translatedAttributeLabels($language), array(
106  'name' => Zurmo::t('Core', 'Name', array(), null, $language),
107  'role' => Zurmo::t('ZurmoModule', 'Parent Role', array(), null, $language),
108  'roles' => Zurmo::t('ZurmoModule', 'Roles', array(), null, $language),
109  'users' => Zurmo::t('UsersModule', 'Users', array(), null, $language)
110  ));
111  }
112 
113  public static function getDefaultMetadata()
114  {
115  $metadata = parent::getDefaultMetadata();
116  $metadata[__CLASS__] = array(
117  'members' => array(
118  'name',
119  ),
120  'relations' => array(
121  'role' => array(static::HAS_MANY_BELONGS_TO, 'Role'),
122  'roles' => array(static::HAS_MANY, 'Role'),
123  'users' => array(static::HAS_MANY, 'User'),
124  ),
125  'rules' => array(
126  array('name', 'required'),
127  array('name', 'unique'),
128  array('name', 'type', 'type' => 'string'),
129  array('name', 'length', 'min' => 1, 'max' => 64),
130  ),
131  'defaultSortAttribute' => 'name'
132  );
133  return $metadata;
134  }
135 
136  public static function isTypeDeletable()
137  {
138  return true;
139  }
140 
141  public static function getModuleClassName()
142  {
143  return 'RolesModule';
144  }
145 
146  protected function forgetPermissionsRightsAndPoliciesCache()
147  {
148  PermissionsCache::forgetAll();
149  Permission::resetCaches();
150  RightsCache::forgetAll();
151  PoliciesCache::forgetAll();
152  }
153 
154  protected function afterSave()
155  {
156  if (((isset($this->originalAttributeValues['role'])) || $this->isNewModel) &&
157  $this->role != null && $this->role->id > 0)
158  {
161  }
162  if (isset($this->originalAttributeValues['role']) && $this->originalAttributeValues['role'][1] > 0)
163  {
164  $this->forgetPermissionsRightsAndPoliciesCache();
165  }
166  static::$roleIdToRoleCache[intval($this->id)] = $this;
167  parent::afterSave();
168  }
169 
170  protected function beforeSave()
171  {
172  if (parent::beforeSave())
173  {
174  if (isset($this->originalAttributeValues['role']) && $this->originalAttributeValues['role'][1] > 0)
175  {
176  //copy to new object, so we can populate the old parent role as the related role.
177  //otherwise it gets passed by reference. We need the old $this->role information to properly
178  //utilize the roleParentBeingRemoved method.
179  $role = unserialize(serialize($this));
180  $role->role = Role::getById($this->originalAttributeValues['role'][1]);
183  assert('$this->originalAttributeValues["role"][1] != $this->role->id');
184  }
185  return true;
186  }
187  else
188  {
189  return false;
190  }
191  }
192 
193  protected function beforeDelete()
194  {
195  if (!parent::beforeDelete())
196  {
197  return false;
198  }
200  return true;
201  }
202 
203  protected function afterDelete()
204  {
205  $this->forgetPermissionsRightsAndPoliciesCache();
207  AllPermissionsOptimizationCache::forgetAll();
208  static::forgetRoleIdToRoleCache();
209  }
210 
211  protected function beforeValidate()
212  {
213  if (!$this->checkIfParentIsNotInChildRoles($this->roles))
214  {
215  $errorMessage = Zurmo::t('ZurmoModule', 'You cannot select a child role for the parent role');
216  $this->addError('role', $errorMessage);
217  return false;
218  }
219  return parent::beforeValidate();
220  }
221 
227  protected function checkIfParentIsNotInChildRoles($roles)
228  {
229  $isNotInChildRoles = true;
230  foreach ($roles as $role)
231  {
232  if ($this->role->isSame($role))
233  {
234  $isNotInChildRoles = false;
235  }
236  else
237  {
238  $isNotInChildRoles &= $this->checkIfParentIsNotInChildRoles($role->roles);
239  }
240  }
241  return $isNotInChildRoles;
242  }
243  }
244 ?>
Definition: Role.php:37
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
static getByName($name)
Definition: Role.php:45
Definition: Item.php:37
static getById($id, $modelClassName=null)
addError($attributeName, $errorMessage)
checkIfParentIsNotInChildRoles($roles)
Definition: Role.php:227
Generated on Tue Sep 22 2020 07:10:37
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.