public static addOwnerHistory ( |
||
$farm | Farm object | |
$owner | New owner | |
$user | User who changes owner for this farm |
public static function addOwnerHistory(Farm $farm, User $owner, User $user)
{
$history = unserialize($farm->settings[self::OWNER_HISTORY]);
if (!is_array($history)) {
$history = [];
}
$history[] = ['newId' => $owner->id, 'newEmail' => $owner->email, 'changedById' => $user->id, 'changedByEmail' => $user->email, 'dt' => date('Y-m-d H:i:s')];
$farm->settings[self::OWNER_HISTORY] = serialize($history);
}
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; } }