/** * Checks if the provided $roleId is allowed to perform the given $action. * * @param int $roleId Role ID * @param string $action Action to check: create, edit, delete or publish * @return bool */ public function checkPermission($roleId, $action) { if ($roleId == ROLE_ID_ADMINISTRATOR) { return true; } $this->_loadPermissions(); $rule = $this->_permissions->filter(function ($rule) use($roleId, $action) { return $rule->get('role_id') == $roleId && $rule->get('action') == $action; })->first(); return !empty($rule); }
/** * Return all blocks to render in the given row column * * @param int $column Numeric column index, starting with 1 * @return array */ public function getBlocksForColumn($column) { $blocks = new \Cake\Collection\Collection($this->cms_blocks); return $blocks->filter(function ($block) use($column) { return $block->column_index == $column; })->toArray(); }
public function isPermitted($permissions, $operator = OPERATOR_AND) { $session = $this->request->session()->read('Auth.User'); if ($session['idr_admin']) { return true; } $acoes = new Collection($session['acoes']); if (is_array($permissions)) { if ($operator == OPERATOR_AND) { foreach ($permissions as $k => $p) { $permitido = $acoes->filter(function ($acao, $key) use($p) { return mb_strtoupper($acao['tag']) == mb_strtoupper($p); }); if (count($permitido->toArray()) == 0) { break; } } } else { foreach ($permissions as $k => $p) { $permitido = $acoes->filter(function ($acao, $key) use($p) { return mb_strtoupper($acao['tag']) == mb_strtoupper($p); }); if (count($permitido->toArray()) > 0) { break; } } } } else { $permitido = $acoes->filter(function ($acao, $key) use($permissions) { return mb_strtoupper($acao['tag']) == mb_strtoupper($permissions); }); } if (count($permitido->toArray()) > 0) { return true; } else { return false; } }
private function anyPermitted($permissions, $operator = self::OPERATOR_AND) { $session = $this->request->session()->read('Auth.User'); if ($session['idr_admin']) { return true; } $acoes = new Collection($session['acoes']); if (is_array($permissions)) { if ($operator == self::OPERATOR_AND) { foreach ($permissions as $k => $p) { $permitido = $acoes->filter(function ($acao, $key) use($p) { return mb_strtoupper($acao['tag']) == mb_strtoupper($p); }); if (count($permitido->toArray()) == 0) { break; } } } else { foreach ($permissions as $k => $p) { $permitido = $acoes->filter(function ($acao, $key) use($p) { return mb_strtoupper($acao['tag']) == mb_strtoupper($p); }); if (count($permitido->toArray()) > 0) { break; } } } } else { $permitido = $acoes->filter(function ($acao, $key) use($permissions) { return mb_strtoupper($acao['tag']) == mb_strtoupper($permissions); }); } if (count($permitido->toArray()) == 0) { throw new UnauthorizedException("Usuário da sessão não possui permissão para acessar a ação escolhida"); } }
/** * Tests that it is possible to chain filter() as it returns a collection object * * @return void */ public function testFilterChaining() { $this->assertFalse(defined('HHVM_VERSION'), 'Broken on HHVM'); $items = ['a' => 1, 'b' => 2, 'c' => 3]; $collection = new Collection($items); $callable = $this->getMock('stdClass', ['__invoke']); $callable->expects($this->once())->method('__invoke')->with(3, 'c'); $filtered = $collection->filter(function ($value, $key, $iterator) { return $value > 2; }); $this->assertInstanceOf('\\Cake\\Collection\\Collection', $filtered); $filtered->each($callable); }
/** * Retrieves a type that should be used for a specific field * * @param string $field Name of field * @param string $type User-specified type * @return string */ public function getType($field, $type) { $reflector = new ReflectionClass('Phinx\\Db\\Adapter\\AdapterInterface'); $collection = new Collection($reflector->getConstants()); $validTypes = $collection->filter(function ($value, $constant) { $value; return substr($constant, 0, strlen('PHINX_TYPE_')) === 'PHINX_TYPE_'; })->toArray(); $fieldType = $type; if ($type === null || !in_array($type, $validTypes)) { if ($type === 'primary') { $fieldType = 'integer'; } elseif ($field === 'id') { $fieldType = 'integer'; } elseif (in_array($field, ['created', 'modified', 'updated'])) { $fieldType = 'datetime'; } else { $fieldType = 'string'; } } return $fieldType; }
/** * Tests that it is possible to chain filter() as it returns a collection object * * @return void */ public function testFilterChaining() { $items = ['a' => 1, 'b' => 2, 'c' => 3]; $collection = new Collection($items); $callable = $this->getMockBuilder(\StdClass::class)->setMethods(['__invoke'])->getMock(); $callable->expects($this->once())->method('__invoke')->with(3, 'c'); $filtered = $collection->filter(function ($value, $key, $iterator) { return $value > 2; }); $this->assertInstanceOf('Cake\\Collection\\Collection', $filtered); $filtered->each($callable); }
/** * This method is called in case a primary key was defined using the addPrimaryKey() method. * It currently does something only if using SQLite. * If a column is an auto-increment key in SQLite, it has to be a primary key and it has to defined * when defining the column. Phinx takes care of that so we have to make sure columns defined as autoincrement were * not added with the addPrimaryKey method, otherwise, SQL queries will be wrong. * * @return void */ protected function filterPrimaryKey() { if ($this->getAdapter()->getAdapterType() !== 'sqlite' || empty($this->options['primary_key'])) { return; } $primaryKey = $this->options['primary_key']; if (!is_array($primaryKey)) { $primaryKey = [$primaryKey]; } $primaryKey = array_flip($primaryKey); $columnsCollection = new Collection($this->columns); $primaryKeyColumns = $columnsCollection->filter(function ($columnDef, $key) use($primaryKey) { return isset($primaryKey[$columnDef->getName()]); })->toArray(); if (empty($primaryKeyColumns)) { return; } foreach ($primaryKeyColumns as $primaryKeyColumn) { if ($primaryKeyColumn->isIdentity()) { unset($primaryKey[$primaryKeyColumn->getName()]); } } $primaryKey = array_flip($primaryKey); if (!empty($primaryKey)) { $this->options['primary_key'] = $primaryKey; } else { unset($this->options['primary_key']); } }
protected function _getLosers() { // $gameMembershipsCollection = new Collection($this->game_memberships); // $victor_member = $gameMembershipsCollection->indexBy('id')->max('points'); // // $victor['full_name'] = Hash::get($victor_member, 'member.full_name'); // $victor['id'] = Hash::get($victor_member, 'member.id'); $gm_collection = new Collection($this->game_memberships); $victor = $gm_collection->indexBy('id')->max('points'); $victor_id = $victor->generic_member->id; $losers = $gm_collection->filter(function ($gm, $key) use($victor_id) { return $victor_id != $gm->generic_member->id; }); // if(!$loser = $losers->first()) // return false; // if(!($loser->member or $loser->temp_member)) // return false; $losers_array = []; foreach ($losers as $loser) { array_push($losers_array, $loser); } return $losers_array; }