/** Ask if user has right to do this * @param string $method One of created, browsed * @param System\User $user User to get perms for * @return bool */ public static function can_user($method, \System\User $user) { if ($user->is_root()) { return true; } $cname = get_called_class(); $conds = array(); if (isset($cname::$access) && isset($cname::$access[$method]) && !is_null($cname::$access[$method])) { return !!$cname::$access[$method]; } if ($user->is_guest()) { $conds['public'] = true; } else { $groups = $user->get_groups(); if (any($groups)) { $conds[] = 'id_group IN (' . implode(',', collect_ids($groups)) . ')'; } } $perm = \System\User\Perm::get_first()->add_filter(array('attr' => 'trigger', 'type' => 'in', 'in' => array('model-' . $method, '*')))->add_filter(array('attr' => 'name', 'type' => 'in', 'in' => array(\System\Loader::get_model_from_class($cname) . \System\Loader::SEP_MODEL . $method, '*')))->where($conds)->fetch(); return $perm ? $perm->allow : static::get_default_for($method); }
/** Get all users permissions * @return array Set of permissions (System\User\Perm) */ public function get_rights() { if (is_null($this->rights)) { if ($this->id) { $conds = array("public" => true); $ids = collect_ids($this->groups->fetch()); if (any($ids)) { $conds[] = "id_group IN (" . implode(',', $ids) . ")"; } $this->rights = \System\User\Perm::get_all()->where($conds, "t0", true)->reset_cols()->add_cols(array("trigger", "id_system_user_perm"), "t0")->assoc_with('')->fetch('trigger', 'id_system_user_perm'); } else { $this->rights = array(); } } return $this->rights; }
/** * Get all available modules * * @param bool $with_perms * @return array */ public static function get_all($with_perms = false) { $mods = array(); $path = ROOT . self::DIR; \System\Directory::find_all_files($path, $mods, '/\\.php$/'); sort($mods); foreach ($mods as &$mod) { $mod = array("path" => preg_replace('/\\.php$/', '', substr($mod, strlen($path) + 1))); if ($with_perms) { $mod['perms'] = \System\User\Perm::get_all()->where(array("type" => 'module', "trigger" => $mod['path']))->fetch(); } } return $mods; }