Пример #1
0
 /**
  * @param   JsonData    $ids
  * @param   string      $action
  * @param   int         $ownerId
  */
 public function xGroupActionHandlerAction(JsonData $ids, $action, $ownerId = null)
 {
     $processed = array();
     $errors = array();
     $needUpdateFarmOwner = false;
     $actionMsg = '';
     $ids = (array) $ids;
     if ($ownerId && !User::findOne([['id' => $ownerId], ['accountId' => $this->user->getAccountId()]])) {
         $ownerId = null;
     }
     foreach ($ids as $userId) {
         try {
             $user = Scalr_Account_User::init();
             $user->loadById($userId);
             switch ($action) {
                 case 'delete':
                     $actionMsg = 'removed';
                     if ($this->user->canRemoveUser($user)) {
                         $ownedFarms = Farm::findByOwnerId($user->id);
                         if ($ownedFarms->count() > 0) {
                             if ($ownerId) {
                                 /* @var $newOwner User */
                                 $newOwner = User::findPk($ownerId);
                                 /* @var $u User */
                                 $u = User::findPk($this->user->getId());
                                 foreach ($ownedFarms as $farm) {
                                     /* @var $farm Farm */
                                     FarmSetting::addOwnerHistory($farm, $newOwner, $u);
                                     $farm->ownerId = $ownerId;
                                     $farm->save();
                                 }
                             } else {
                                 $needUpdateFarmOwner = true;
                                 throw new Exception("You can't delete owner of the Farm");
                             }
                         }
                         $user->delete();
                         $processed[] = $user->getId();
                     } else {
                         throw new Exception('Insufficient permissions to remove user');
                     }
                     break;
                 case 'activate':
                     $actionMsg = 'activated';
                     if ($this->user->getId() !== $user->getId() && $this->user->canEditUser($user)) {
                         if ($user->status == Scalr_Account_User::STATUS_ACTIVE) {
                             throw new Scalr_Exception_Core('User(s) has already been activated');
                         }
                         $user->status = Scalr_Account_User::STATUS_ACTIVE;
                         $user->save();
                         $processed[] = $user->getId();
                     } else {
                         throw new Scalr_Exception_Core('Insufficient permissions to activate user');
                     }
                     break;
                 case 'deactivate':
                     $actionMsg = 'deactivated';
                     if ($this->user->getId() !== $user->getId() && $this->user->canEditUser($user)) {
                         if ($user->status == Scalr_Account_User::STATUS_INACTIVE) {
                             throw new Scalr_Exception_Core('User(s) has already been suspended');
                         }
                         $user->status = Scalr_Account_User::STATUS_INACTIVE;
                         $user->save();
                         $processed[] = $user->getId();
                     } else {
                         throw new Scalr_Exception_Core('Insufficient permissions to deactivate user');
                     }
                     break;
             }
         } catch (Exception $e) {
             $errors[] = $e->getMessage();
         }
     }
     $num = count($ids);
     if (count($processed) == $num) {
         $this->response->success("Selected user(s) successfully {$actionMsg}");
     } else {
         array_walk($errors, function (&$item) {
             $item = '- ' . $item;
         });
         $this->response->warning(sprintf("Successfully {$actionMsg} only %d from %d users. \nFollowing errors occurred:\n%s", count($processed), $num, join(array_unique($errors), "\n")));
     }
     $this->response->data(['processed' => $processed]);
     if ($needUpdateFarmOwner) {
         $users = [];
         foreach (User::findByAccountId($this->user->getAccountId()) as $user) {
             /* @var $user User */
             if (in_array($user->id, $ids)) {
                 continue;
             }
             $users[] = ['id' => $user->id, 'email' => $user->email];
         }
         $this->response->data(['needUpdateFarmOwner' => $needUpdateFarmOwner, 'usersList' => $users]);
     }
 }