/** * {@inheritdoc} */ public static function loadWithPermissions($token, $context) { $entity = intval($context); $table = new static(); // Find users matching 3 criterias + the given token: // - if the user has access within the given context; // - if the user is a site admin within the given context; // - if the user is a system admin $select = $table->select()->setIntegrityCheck(false)->from(['us' => 'user'])->join(['ue' => 'user_to_entity'], 'us.id = ue.user_id', [])->join(['en' => 'entity'], 'ue.entity_id = en.id', ['en.id as entity'])->where('us.token = ?', $token)->where("en.id = ? OR us.admin = true", $entity); $model = $table->fetchRow($select->limit(1)); if (!$model) { // user not found OR user can't access the given context return null; } if ($model->admin) { // can access everything… $collection = [$model]; } else { // filter the user against the groups he belongs to… $select->join(['ug' => 'user_to_group'], 'us.id = ug.user_id', [])->join(['gr' => 'group'], 'ug.group_id = gr.id', ['gr.id as gid', 'gr.admin as gadmin'])->where('gr.entity_id = ?', $entity); $collection = $table->fetchAll($select); } $permissions = []; foreach ($collection as $model) { if ($model->admin) { break; } if (!isset($permissions[$model->entity])) { $permissions[$model->entity] = []; } $permissions[$model->entity][] = [$model->gid, intval($model->gadmin)]; } $model->permissions = $permissions; return $model; }
/** * Fetches one row in an object of type Benri_Db_Table_Row, or returns * null if no row matches the specified criteria. * * @param string $column The sql `where` clause * @param mixed $value The value to use against the `where` clause * @return Benri_Db_Table_Row or null The row results, or null if no row * found */ public static function locate($column, $value) { $table = new static(); $select = $table->select()->where("{$table->getAdapter()->quoteIdentifier($column)} = ?", $value)->limit(1); return $table->fetchRow($select); }