protected function linkUnlinkProcess(Model $model, $link = true, array $extra = [])
 {
     if ($this->primaryModel->pk === null) {
         throw new Exception('Unable to unlink models: the primary key of ' . get_class($this->primaryModel) . ' is null.');
     }
     $method = $link ? 'insert' : 'delete';
     if ($this->primaryModel->pk === null) {
         throw new Exception('Unable to ' . ($link ? 'link' : 'unlink') . ' models: the primary key of ' . get_class($this->primaryModel) . ' is null.');
     }
     if ($this->through && $link) {
         $throughModel = new $this->through();
         list($through, $created) = $throughModel->objects()->getOrCreate([$this->primaryModelColumn => $this->primaryModel->pk, $this->modelColumn => $model->pk]);
         return $through->pk;
     } else {
         $db = $this->primaryModel->getDb();
         /** @var $command \Mindy\Query\Command */
         $command = $db->createCommand()->{$method}($this->relatedTable, array_merge([$this->primaryModelColumn => $this->primaryModel->pk, $this->modelColumn => $model->pk], $extra));
         return $command->execute();
     }
 }
Example #2
0
 public function login(Model $model, $duration = null)
 {
     $time = $model->getDb()->getQueryBuilder()->convertToDateTime(time());
     $model->last_login = $time;
     $model->save(['last_login']);
     if ($duration === null) {
         $duration = Mindy::app()->getModule('User')->loginDuration;
     }
     $this->saveToCookie($model, $duration);
     if ($this->absoluteAuthTimeout) {
         $this->getStorage()->add(self::AUTH_ABSOLUTE_TIMEOUT_VAR, time() + $this->absoluteAuthTimeout);
     }
     $model->setIsGuest(false);
     $this->setModel($model);
     $sessionId = Mindy::app()->session->getId();
     $session = Session::objects()->get(['id' => $sessionId]);
     if ($session) {
         $session->user = $model;
         $session->save();
     }
     $this->getEventManager()->send($this, 'onAuth', $model);
     if (class_exists('\\Modules\\UserActions\\Models\\UserLog')) {
         \Modules\UserActions\Models\UserLog::objects()->create(['message' => UserModule::t('User <a href="{url}">{name}</a> logged in', ['{url}' => $model->getAbsoluteUrl(), '{name}' => (string) $model]), 'module' => $model->getModuleName(), 'ip' => $model->getIp(), 'user' => $model]);
     }
     return !$this->getIsGuest();
 }