/** * Loads authorization data from persistent storage. */ protected function load() { $this->children = []; $this->rules = []; $this->assignments = []; $this->items = []; $items = $this->loadFromFile($this->itemFile); $itemsMtime = @filemtime($this->itemFile); $assignments = $this->loadFromFile($this->assignmentFile); $assignmentsMtime = @filemtime($this->assignmentFile); $rules = $this->loadFromFile($this->ruleFile); foreach ($items as $name => $item) { $class = $item['type'] == Item::TYPE_PERMISSION ? Permission::className() : Role::className(); $this->items[$name] = new $class(['name' => $name, 'description' => isset($item['description']) ? $item['description'] : null, 'ruleName' => isset($item['ruleName']) ? $item['ruleName'] : null, 'data' => isset($item['data']) ? $item['data'] : null, 'createdAt' => $itemsMtime, 'updatedAt' => $itemsMtime]); } foreach ($items as $name => $item) { if (isset($item['children'])) { foreach ($item['children'] as $childName) { if (isset($this->items[$childName])) { $this->children[$name][$childName] = $this->items[$childName]; } } } } foreach ($assignments as $userId => $roles) { foreach ($roles as $role) { $this->assignments[$userId][$role] = new Assignment(['userId' => $userId, 'roleName' => $role, 'createdAt' => $assignmentsMtime]); } } foreach ($rules as $name => $ruleData) { $this->rules[$name] = unserialize($ruleData); } }
/** * Populates an auth item with the data fetched from database * * @param array $row the data from the auth item table * @return Item the populated auth item instance (either Role or Permission) */ protected function populateItem($row) { $class = $row['type'] == Item::TYPE_PERMISSION ? Permission::className() : Role::className(); if (!isset($row['data']) || ($data = @unserialize($row['data'])) === false) { $data = null; } return new $class(['name' => $row['name'], 'type' => $row['type'], 'description' => $row['description'], 'ruleName' => $row['rule_name'], 'data' => $data, 'createdAt' => $row['created_at'], 'updatedAt' => $row['updated_at']]); }