public function _owner($from, $to, $action) { switch ($action) { case static::ACT_CONVERT_TO_OBJECT: /* @var $from Farm */ $to->owner = ['id' => $from->ownerId]; break; case static::ACT_CONVERT_TO_ENTITY: /* @var $to Farm */ $owner = ApiController::getBareId($from, 'owner'); if (!isset($owner)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed owner.id property"); } if (!empty($to->ownerId) && $to->ownerId != $owner) { $this->controller->checkPermissions($to, Acl::PERM_FARMS_CHANGE_OWNERSHIP); $user = User::findOne([['id' => $owner], ['accountId' => $this->controller->getUser()->getAccountId()]]); /* @var $user User */ if (!$user) { throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Requested User either does not exist or is not owned by current account"); } $to->createdByEmail = $user->getEmail(); FarmSetting::addOwnerHistory($to, $user, $this->controller->getUser()); } $to->ownerId = $owner; break; case static::ACT_GET_FILTER_CRITERIA: $owner = ApiController::getBareId($from, 'owner'); return [['ownerId' => $owner]]; break; } }
/** * @functional */ public function testUser() { $userId = \Scalr::config('scalr.phpunit.userid'); $envId = \Scalr::config('scalr.phpunit.envid'); if (empty($userId) || empty($envId)) { $this->markTestSkipped("This test requires valid database connection so it's considered to be functional."); } /* @var $user User */ $user = User::findOne([['id' => $userId]]); $this->assertInstanceOf('Scalr\\Model\\Entity\\Account\\User', $user); $email = $user->getSetting(UserSetting::NAME_GRAVATAR_EMAIL); $this->assertNotEmpty($email); $environment = Environment::findPk($envId); $this->assertInstanceOf('Scalr\\Model\\Entity\\Account\\Environment', $environment); $entityIterator = User::result(User::RESULT_ENTITY_ITERATOR)->find(null, null, null, 10); $this->assertInstanceOf('Scalr\\Model\\Collections\\EntityIterator', $entityIterator); $this->assertNotEmpty($entityIterator->count()); $this->assertNotEmpty($entityIterator->getArrayCopy()); foreach ($entityIterator->filterByType(User::TYPE_ACCOUNT_OWNER) as $item) { $this->assertEquals(User::TYPE_ACCOUNT_OWNER, $item->type); } foreach (User::result(User::RESULT_ENTITY_ITERATOR)->findByType(User::TYPE_SCALR_ADMIN) as $item) { $this->assertEquals(User::TYPE_SCALR_ADMIN, $item->type); } $arrayCollection = User::result(User::RESULT_ENTITY_COLLECTION)->find(null, null, null, 10); $this->assertInstanceOf('Scalr\\Model\\Collections\\ArrayCollection', $arrayCollection); $this->assertNotEmpty($arrayCollection->count()); $this->assertNotEmpty($arrayCollection->getArrayCopy()); $rs = User::result(User::RESULT_RAW)->find(null, null, null, 10); $this->assertInstanceOf('ADORecordSet', $rs); foreach ($rs as $item) { $this->assertNotEmpty($item); $this->assertInternalType('array', $item); } }
public function _owner($from, $to, $action) { switch ($action) { case static::ACT_CONVERT_TO_OBJECT: /* @var $from Farm */ $to->owner = ['id' => $from->createdById]; break; case static::ACT_CONVERT_TO_ENTITY: /* @var $to Farm */ $owner = ApiController::getBareId($from, 'owner'); if (!isset($owner)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed owner.id property"); } if (!empty($to->createdById) && $to->createdById != $owner) { $this->controller->checkPermissions($to, Acl::PERM_FARMS_CHANGE_OWNERSHIP); $user = User::findOne([['id' => $owner], ['accountId' => $this->controller->getUser()->getAccountId()]]); /* @var $user User */ if (!$user) { throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Requested User either does not exist or is not owned by current account"); } $to->createdByEmail = $user->getEmail(); $history = unserialize($to->settings[FarmSetting::OWNER_HISTORY]); if (!is_array($history)) { $history = []; } $history[] = ['newId' => $owner, 'newEmail' => $to->createdByEmail, 'changedById' => $this->controller->getUser()->getId(), 'changedByEmail' => $this->controller->getUser()->getEmail(), 'dt' => date('Y-m-d H:i:s')]; // TODO: move to subclass \Farm\Setting\OwnerHistory $to->settings[FarmSetting::OWNER_HISTORY] = serialize($history); } $to->createdById = $owner; break; case static::ACT_GET_FILTER_CRITERIA: $owner = ApiController::getBareId($from, 'owner'); return [['createdById' => $owner]]; break; } }
/** * {@inheritdoc} * @see \Scalr\System\Zmq\Cron\TaskInterface::worker() */ public function worker($request) { //Warming up static DI cache \Scalr::getContainer()->warmup(); // Reconfigure observers \Scalr::ReconfigureObservers(); try { $dbFarm = DBFarm::LoadByID($request->farmId); $curDate = new DateTime(); $tdValue = $dbFarm->GetSetting(Entity\FarmSetting::LEASE_TERMINATE_DATE); if ($tdValue) { $td = new DateTime($tdValue); if ($td < $curDate) { //Terminates farm SettingEntity::increase(SettingEntity::LEASE_TERMINATE_FARM); //Ajdusts both account & environment for the audit log \Scalr::getContainer()->auditlogger->setAccountId($dbFarm->ClientID)->setAccountId($dbFarm->EnvID); \Scalr::FireEvent($request->farmId, new FarmTerminatedEvent(0, 1, false, 1, true, null)); $this->log('INFO', sprintf('Farm: %s [ID: %d] was terminated by lease manager', $dbFarm->Name, $dbFarm->ID)); } else { // only inform user $days = $td->diff($curDate)->days; $notifications = json_decode($dbFarm->GetSetting(Entity\FarmSetting::LEASE_NOTIFICATION_SEND), true); $governance = new Scalr_Governance($dbFarm->EnvID); $settings = $governance->getValue(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_LEASE, 'notifications'); if (is_array($settings)) { foreach ($settings as $n) { if (!$notifications[$n['key']] && $n['period'] >= $days) { $mailer = \Scalr::getContainer()->mailer; $tdHuman = Scalr_Util_DateTime::convertDateTime($td, $dbFarm->GetSetting(Entity\FarmSetting::TIMEZONE), 'M j, Y'); /* @var $user Entity\Account\User */ if ($n['to'] == 'owner') { if ($dbFarm->ownerId) { $user = Entity\Account\User::findPk($dbFarm->ownerId); } else { $user = Entity\Account\User::findOne([['accountId' => $dbFarm->ClientID], ['type' => Entity\Account\User::TYPE_ACCOUNT_OWNER]]); } if (\Scalr::config('scalr.auth_mode') == 'ldap') { $email = $user->getSetting(Entity\Account\User\UserSetting::NAME_LDAP_EMAIL); if (!$email) { $email = $user->email; } } else { $email = $user->email; } $mailer->addTo($email); } else { foreach (explode(',', $n['emails']) as $email) { $mailer->addTo(trim($email)); } } $mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/farm_lease_terminate.eml.php', array('terminateDate' => $tdHuman, 'farm' => $dbFarm->Name, 'envName' => $dbFarm->GetEnvironmentObject()->name, 'envId' => $dbFarm->GetEnvironmentObject()->id, 'showOwnerWarning' => !$dbFarm->ownerId)); $notifications[$n['key']] = 1; $dbFarm->SetSetting(Entity\FarmSetting::LEASE_NOTIFICATION_SEND, json_encode($notifications)); $this->log('INFO', "Notification was sent by key: %s about farm: %s [ID: %d] by lease manager", $n['key'], $dbFarm->Name, $dbFarm->ID); } } } } } } catch (Exception $e) { throw $e; } return $request; }
/** * @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]); } }