protected function hasPermission($permission, Model $requester)
 {
     if ($permission == 'create') {
         return true;
     }
     $idProp = $this->userPropertyForProfileId();
     if (in_array($permission, ['view', 'edit']) && $this->id() == $requester->{$idProp}) {
         return true;
     }
     return $requester->isAdmin();
 }
Esempio n. 2
0
 public function deleteModel(Model $model)
 {
     $tablename = $model->getTablename();
     try {
         return $this->app['db']->delete($tablename)->where($model->ids())->execute() instanceof PDOStatement;
     } catch (PDOException $original) {
         $e = new AdapterException('An error occurred in the database adapter while deleting the ' . $model::modelName());
         $e->setException($original);
         throw $e;
     }
 }
 protected function hasPermission($permission, Model $requester)
 {
     // always allow new user registrations
     if ($permission == 'create') {
         return true;
     }
     // users can only edit themselves
     if ($permission === 'edit' && $requester instanceof self && $requester->id() == $this->id()) {
         return true;
     }
     // otherwise, defer to admin permissions
     return $requester->isAdmin();
 }
 public function __construct($app)
 {
     $config = $app['config'];
     // make the app available to models
     Model::inject($app);
     // set up the model driver
     $class = $config->get('models.driver');
     $this->driver = new $class($app);
     Model::setDriver($this->driver);
     // used for password hasing
     Validate::configure(['salt' => $config->get('app.salt')]);
 }
Esempio n. 5
0
 public function __construct($app)
 {
     // make the locale available to models
     Model::setLocale($app['locale']);
     // set up the model adapter
     $config = $app['config'];
     $class = $config->get('models.adapter');
     $this->adapter = new $class($app);
     Model::setAdapter($this->adapter);
     // used for password hasing
     Validator::configure(['salt' => $config->get('app.salt')]);
 }
Esempio n. 6
0
 /**
  * Detaches a child model from this model.
  *
  * @param Model $model child model
  *
  * @return self
  */
 public function detach(Model $model)
 {
     $model->{$this->foreignKey} = null;
     $model->save();
     return $this;
 }
Esempio n. 7
0
 protected function initialize()
 {
     parent::initialize();
     self::creating(['Infuse\\Auth\\Models\\UserLink', 'generateLink']);
 }
 protected function hasPermission($permission, Model $requester)
 {
     return $requester->isAdmin();
 }
Esempio n. 9
0
 public function save(Model $model)
 {
     $model->save();
     $this->attach($model);
     return $model;
 }
Esempio n. 10
0
 /**
  * Attaches a child model to this model.
  *
  * @param Model $model child model
  *
  * @return self
  */
 public function attach(Model $model)
 {
     $model->{$this->foreignKey} = $this->localModel->{$this->localKey};
     $model->save();
     return $this;
 }
Esempio n. 11
0
 /**
  * Attaches a model to the relationship by creating
  * a pivot model.
  *
  * @param Model $model
  *
  * @return self
  */
 public function attach(Model $model)
 {
     // create pivot relation
     $pivot = new Pivot();
     $pivot->setTablename($this->tablename);
     // build the local side
     $ids = $this->localModel->ids();
     foreach ($ids as $property => $id) {
         $pivot->{$this->localKey} = $id;
     }
     // build the foreign side
     $ids = $model->ids();
     foreach ($ids as $property => $id) {
         $pivot->{$this->foreignKey} = $id;
     }
     $pivot->save();
     $model->pivot = $pivot;
     return $this;
 }
 /**
  * Serializes a model to an array.
  *
  * @param Model $model model to be serialized
  *
  * @return array properties
  */
 public function toArray(Model $model)
 {
     // start with the base representation of the model
     $result = $model->toArray();
     // apply namespacing to excluded properties
     $namedExc = [];
     foreach ($this->exclude as $k) {
         array_set($namedExc, $k, true);
     }
     // apply namespacing to included properties
     $namedInc = [];
     foreach ($this->include as $k) {
         array_set($namedInc, $k, true);
     }
     // apply namespacing to expanded properties
     $namedExp = [];
     foreach ($this->expand as $k) {
         array_set($namedExp, $k, true);
     }
     // remove excluded properties
     foreach (array_keys($result) as $k) {
         if (isset($namedExc[$k]) && !is_array($namedExc[$k])) {
             unset($result[$k]);
         }
     }
     // add included properties
     foreach (array_keys($namedInc) as $k) {
         if (!isset($result[$k]) && isset($namedInc[$k])) {
             $result[$k] = $model->{$k};
         }
     }
     // expand any relational model properties
     $result = $this->expand($model, $result, $namedExc, $namedInc, $namedExp);
     // apply hooks, if available
     if (method_exists($model, 'toArrayHook')) {
         $model->toArrayHook($result, $namedExc, $namedInc, $namedExp);
     }
     // order the properties array by name for consistency
     // since it is constructed in a random order
     ksort($result);
     return $result;
 }