All Data Structures Functions Variables Pages
UserStatusUtil.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 
43  {
44  const ACTIVE = 'Active';
45 
46  const INACTIVE = 'Inactive';
47 
52  public static function makeByUser(User $user)
53  {
54  $userStatus = new UserStatus();
55  if ( Right::DENY == $user->getExplicitActualRight ('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB) &&
56  Right::DENY == $user->getExplicitActualRight ('UsersModule', UsersModule::RIGHT_LOGIN_VIA_MOBILE) &&
57  Right::DENY == $user->getExplicitActualRight ('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API))
58  {
59  $userStatus->setInactive();
60  }
61  return $userStatus;
62  }
63 
68  public static function getSelectedValueByUser(User $user)
69  {
70  $userStatus = self::makeByUser($user);
71  if ($userStatus->isActive())
72  {
73  return self::ACTIVE;
74  }
75  else
76  {
77  return self::INACTIVE;
78  }
79  }
80 
84  public static function makeByPostData($postData)
85  {
86  assert('is_array($postData)');
87  $userStatus = new UserStatus();
88  if (!isset($postData['userStatus']))
89  {
90  return null;
91  }
92  elseif ($postData['userStatus'] == self::ACTIVE)
93  {
94  return $userStatus;
95  }
96  elseif ($postData['userStatus'] == self::INACTIVE)
97  {
98  $userStatus->setInactive();
99  return $userStatus;
100  }
101  else
102  {
103  throw new NotSupportedException();
104  }
105  }
106 
112  public static function removeIfExistsFromPostData($postData)
113  {
114  assert('is_array($postData)');
115  if (isset($postData['userStatus']))
116  {
117  unset($postData['userStatus']);
118  }
119  return $postData;
120  }
121 
127  public static function resolveUserStatus(User $user, UserStatus $userStatus)
128  {
129  assert('$user->id > 0');
130  if ($userStatus->isActive())
131  {
132  if ( Right::DENY == $user->getExplicitActualRight ('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB) ||
133  Right::DENY == $user->getExplicitActualRight ('UsersModule', UsersModule::RIGHT_LOGIN_VIA_MOBILE) ||
134  Right::DENY == $user->getExplicitActualRight ('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API))
135  {
136  self::removeExplicitDenyRights($user);
137  }
138  }
139  else
140  {
141  if ( Right::DENY != $user->getExplicitActualRight ('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB) ||
142  Right::DENY != $user->getExplicitActualRight ('UsersModule', UsersModule::RIGHT_LOGIN_VIA_MOBILE) ||
143  Right::DENY != $user->getExplicitActualRight ('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API))
144  {
145  self::setExplicitDenyRights($user);
146  }
147  }
148  }
149 
150  protected static function removeExplicitDenyRights(User $user)
151  {
152  assert('$user->id > 0');
153  $user->removeRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB, Right::DENY);
154  $user->removeRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_MOBILE, Right::DENY);
155  $user->removeRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API, Right::DENY);
156  $saved = $user->save();
157  if (!$saved)
158  {
159  throw new NotSupportedException();
160  }
161  }
162 
163  protected static function setExplicitDenyRights(User $user)
164  {
165  assert('$user->id > 0');
166  $user->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB, Right::DENY);
167  $user->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_MOBILE, Right::DENY);
168  $user->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API, Right::DENY);
169  $saved = $user->save();
170  if (!$saved)
171  {
172  throw new NotSupportedException();
173  }
174  }
175 
176  public static function getStatusArray()
177  {
178  $statusData = array(self::ACTIVE, self::INACTIVE);
179  return array_combine($statusData, $statusData);
180  }
181 
189  public static function canUserEditStatusOnAnotherUser(User $user, User $anotherUser)
190  {
191  assert('$user->id > 0');
192  assert('$anotherUser->id > 0');
193  if ($user->isSame($anotherUser))
194  {
195  return false;
196  }
197  if (Group::getByName(Group::SUPER_ADMINISTRATORS_GROUP_NAME)->contains($anotherUser))
198  {
199  return false;
200  }
201  if (!RightsUtil::canUserAccessModule('UsersModule', $user))
202  {
203  return false;
204  }
205  return true;
206  }
207  }
208 ?>
static resolveUserStatus(User $user, UserStatus $userStatus)
getExplicitActualRight($moduleName, $rightName)
Definition: Permitable.php:112
static removeIfExistsFromPostData($postData)
static makeByPostData($postData)
Definition: User.php:37
static canUserAccessModule($moduleClassName, $user)
Definition: RightsUtil.php:103
static getSelectedValueByUser(User $user)
static getByName($name)
Definition: Group.php:57
static canUserEditStatusOnAnotherUser(User $user, User $anotherUser)
static makeByUser(User $user)
isSame(RedBeanModel $model)
Generated on Wed Feb 26 2020 07:10:41