/** * @inheritdoc */ public static function getDb() { if (Configs::instance()->db !== null) { return Configs::instance()->db; } else { return parent::getDb(); } }
/** * Check access route for user. * @param string|array $route * @param integer|User $user * @return boolean */ public static function checkRoute($route, $params = [], $user = null) { $config = Configs::instance(); $r = static::normalizeRoute($route); if ($config->onlyRegisteredRoute && !isset(static::getRegisteredRoutes()[$r])) { return true; } if ($user === null) { $user = Yii::$app->getUser(); } $userId = $user instanceof User ? $user->getId() : $user; if ($config->strict) { if ($user->can($r, $params)) { return true; } while (($pos = strrpos($r, '/')) > 0) { $r = substr($r, 0, $pos); if ($user->can($r . '/*', $params)) { return true; } } return $user->can('/*', $params); } else { $routes = static::getRoutesByUser($userId); if (isset($routes[$r])) { return true; } while (($pos = strrpos($r, '/')) > 0) { $r = substr($r, 0, $pos); if (isset($routes[$r . '/*'])) { return true; } } return isset($routes['/*']); } }
/** * @inheritdoc */ public function down() { $this->dropTable(Configs::instance()->menuTable); }
/** * Get avalible menu. * @return array */ public function getMenus() { if ($this->_normalizeMenus === null) { $mid = '/' . $this->getUniqueId() . '/'; // resolve core menus $this->_normalizeMenus = []; $config = components\Configs::instance(); $conditions = ['user' => $config->db && $config->db->schema->getTableSchema($config->userTable), 'assignment' => ($userClass = Yii::$app->getUser()->identityClass) && is_subclass_of($userClass, 'yii\\db\\BaseActiveRecord'), 'menu' => $config->db && $config->db->schema->getTableSchema($config->menuTable)]; foreach ($this->_coreItems as $id => $lable) { if (!isset($conditions[$id]) || $conditions[$id]) { $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', $lable), 'url' => [$mid . $id]]; } } foreach (array_keys($this->controllerMap) as $id) { $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', Inflector::humanize($id)), 'url' => [$mid . $id]]; } // user configure menus foreach ($this->_menus as $id => $value) { if (empty($value)) { unset($this->_normalizeMenus[$id]); continue; } if (is_string($value)) { $value = ['label' => $value]; } $this->_normalizeMenus[$id] = isset($this->_normalizeMenus[$id]) ? array_merge($this->_normalizeMenus[$id], $value) : $value; if (!isset($this->_normalizeMenus[$id]['url'])) { $this->_normalizeMenus[$id]['url'] = [$mid . $id]; } } } return $this->_normalizeMenus; }
/** * Get list of application routes * @return array */ public function getAppRoutes($module = null) { if ($module === null) { $module = Yii::$app; } elseif (is_string($module)) { $module = Yii::$app->getModule($module); } $key = [__METHOD__, $module->getUniqueId()]; $cache = Configs::instance()->cache; if ($cache === null || ($result = $cache->get($key)) === false) { $result = []; $this->getRouteRecrusive($module, $result); if ($cache !== null) { $cache->set($key, $result, Configs::instance()->cacheDuration, new TagDependency(['tags' => self::CACHE_TAG])); } } return $result; }
public function down() { $userTable = Configs::instance()->userTable; $this->dropTable($userTable); }