Exemple #1
0
 /**
  * Hash the user password if not yet.
  */
 function beforeSave(array &$errors = array())
 {
     $password = $this->password;
     if (strpos($password, $this->__hashPrefix) !== 0) {
         $this->password = $this->hash($this->username, $password);
     }
     unset($password);
     // note: do not store groups into virtual fields
     if (!empty($this->groups)) {
         $this->__groups = $this->groups;
         unset($this->groups);
     }
     return parent::beforeSave($errors);
 }
Exemple #2
0
 /**
  * @protected
  *
  * Takes care of tasks and settings.
  */
 function beforeSave(array &$errors = array())
 {
     // packUuid
     if ($this->isCreate()) {
         // note; loop until we find a unique uuid
         do {
             $this->identity(Database::fetchField("SELECT UNHEX(REPLACE(UUID(), '-', ''))"));
         } while ($this->find(array($this->primaryKey() => $this->identity())));
     } else {
         if (isset($this->uuid)) {
             $this->uuid = util::packUuid($this->uuid);
         }
     }
     parent::beforeSave($errors);
     if (!$errors) {
         if (empty($this->tasks)) {
             unset($this->tasks);
         } else {
             // Only take task identity and settings
             $this->tasks = array_reduce($this->tasks, function ($result, $task) {
                 $taskModel = new Task($task);
                 $taskModel->load($taskModel->identity());
                 if ($taskModel->identity()) {
                     $result[] = array_filter(array('name' => $taskModel->name, 'version' => $taskModel->version, 'settings' => @$task['settings']));
                 }
                 return $result;
             }, array());
             \core\Log::info('Tasks', $this->tasks);
         }
     }
     return $this;
 }
Exemple #3
0
 /**
  * @protected
  *
  * A TaskInstance can only be saved when,
  *   1. WorkInstance is mutable, or
  *   2. WorkInstance exists and this is creating, or
  *   3. WorkInstance is Open, and nextTask is empty or equals to this one.
  */
 function beforeSave(array &$errors = array())
 {
     $workInstance = $this->workInstance();
     if ($workInstance->immutable()) {
         throw new FrameworkException('Task instance cannot be modified.');
     }
     if (!$workInstance || $workInstance->state == $workInstance::STATE_CLOSE) {
         throw new FrameworkException('Work instance does not exist.');
     }
     if ($this->isCreate()) {
         // note; loop until we find a unique uuid
         do {
             $this->identity(Database::fetchField("SELECT UNHEX(REPLACE(UUID(), '-', ''))"));
         } while ($this->find(array($this->primaryKey() => $this->identity())));
     } else {
         if ($workInstance->nextTask != $this->identity()) {
             throw new FrameworkException('Task is not active.');
         }
     }
     return parent::beforeSave($errors);
 }
Exemple #4
0
 /**
  * Process POST data with current step.
  */
 function beforeSave(array &$errors = array())
 {
     // note; WorkInstance is locked from modification, except when staging and processing.
     if (!$this->isCreate() && $this->_immutable) {
         // todo; Validation messages is not yet put into play.
         $errors[] = 'Cannot modify work instance.';
         throw new FrameworkException('Cannot modify work instance.');
     }
     if ($this->state == static::STATE_CLOSE) {
         $this->nextTask = null;
     }
     // binary encode task uuid
     if (!empty($this->nextTask)) {
         $this->nextTask = util::packUuid($this->nextTask);
     }
     if (isset($this->uuid)) {
         $this->uuid = util::packUuid($this->uuid);
     }
     // reset empty data store
     if (empty($this->dataStore)) {
         unset($this->dataStore);
     }
     // note; task instances will not be created after work instance creation.
     if ($this->isCreate()) {
         // note; loop until we find a unique uuid
         do {
             $this->identity(Database::fetchField("SELECT UNHEX(REPLACE(UUID(), '-', ''))"));
         } while ($this->find(array($this->primaryKey() => $this->identity())));
         // note; Only users with permission to first task can create.
         // note;security; Copy tasks from parent work again.
         if (empty($this->state)) {
             $this->state = static::STATE_OPEN;
         }
         $this->__tasks = array_reduce($this->work()->tasks, function ($result, $task) {
             $task = new TaskInstance($task);
             $task->workInstance($this);
             $task->order = count($result);
             $result[] = $task;
             return $result;
         }, array());
         $task = reset($this->__tasks);
         if (!$task) {
             throw new FrameworkException('Unable to load work tasks.');
         }
         if (!$this->__isInternal && !array_intersect((array) @$this->__request->user->groups, $task->userGroups())) {
             throw new FrameworkException('User has no permission to work on this.');
         }
     }
     unset($this->tasks);
     return parent::beforeSave($errors);
 }