diff --git a/.env.sample b/.env.sample new file mode 100644 index 00000000..ccd0f291 --- /dev/null +++ b/.env.sample @@ -0,0 +1,11 @@ +[app] +debug = false +trace = false + +[database] +hostname = 127.0.0.1 +database = fastadmin +username = root +password = root +hostport = 3306 +prefix = fa_ diff --git a/application/admin/command/Api.php b/application/admin/command/Api.php old mode 100755 new mode 100644 index e5b473af..62cbbf1b --- a/application/admin/command/Api.php +++ b/application/admin/command/Api.php @@ -70,7 +70,7 @@ class Api extends Command } if (version_compare(PHP_VERSION, '7.0.0', '<')) { - if (extension_loaded('opcache')) { + if (extension_loaded('Zend OPcache')) { $configuration = opcache_get_configuration(); $directives = $configuration['directives']; $configName = request()->isCli() ? 'opcache.enable_cli' : 'opcache.enable'; diff --git a/application/admin/controller/Index.php b/application/admin/controller/Index.php index cd05cf64..da62b6c7 100644 --- a/application/admin/controller/Index.php +++ b/application/admin/controller/Index.php @@ -30,21 +30,20 @@ class Index extends Backend public function index() { //左侧菜单 - $menulist = $this->auth->getSidebar([ + list($menulist, $navlist) = $this->auth->getSidebar([ 'dashboard' => 'hot', 'addon' => ['new', 'red', 'badge'], 'auth/rule' => __('Menu'), 'general' => ['new', 'purple'], - ], $this->view->site['fixedpage']); + ], $this->view->site['fixedpage']); $action = $this->request->request('action'); - if ($this->request->isPost()) - { - if ($action == 'refreshmenu') - { - $this->success('', null, ['menulist' => $menulist]); + if ($this->request->isPost()) { + if ($action == 'refreshmenu') { + $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]); } } $this->view->assign('menulist', $menulist); + $this->view->assign('navlist', $navlist); $this->view->assign('title', __('Home')); return $this->view->fetch(); } @@ -55,12 +54,10 @@ class Index extends Backend public function login() { $url = $this->request->get('url', 'index/index'); - if ($this->auth->isLogin()) - { + if ($this->auth->isLogin()) { $this->success(__("You've logged in, do not login again"), $url); } - if ($this->request->isPost()) - { + if ($this->request->isPost()) { $username = $this->request->post('username'); $password = $this->request->post('password'); $keeplogin = $this->request->post('keeplogin'); @@ -75,26 +72,21 @@ class Index extends Backend 'password' => $password, '__token__' => $token, ]; - if (Config::get('fastadmin.login_captcha')) - { + if (Config::get('fastadmin.login_captcha')) { $rule['captcha'] = 'require|captcha'; $data['captcha'] = $this->request->post('captcha'); } $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]); $result = $validate->check($data); - if (!$result) - { + if (!$result) { $this->error($validate->getError(), $url, ['token' => $this->request->token()]); } AdminLog::setTitle(__('Login')); $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0); - if ($result === true) - { + if ($result === true) { Hook::listen("admin_login_after", $this->request); $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]); - } - else - { + } else { $msg = $this->auth->getError(); $msg = $msg ? $msg : __('Username or password is incorrect'); $this->error($msg, $url, ['token' => $this->request->token()]); @@ -102,12 +94,11 @@ class Index extends Backend } // 根据客户端的cookie,判断是否可以自动登录 - if ($this->auth->autologin()) - { + if ($this->auth->autologin()) { $this->redirect($url); } $background = Config::get('fastadmin.login_background'); - $background = stripos($background, 'http')===0 ? $background : config('site.cdnurl') . $background; + $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background; $this->view->assign('background', $background); $this->view->assign('title', __('Login')); Hook::listen("admin_login_init", $this->request); diff --git a/application/admin/controller/user/Rule.php b/application/admin/controller/user/Rule.php index 36189e3e..ae85d485 100644 --- a/application/admin/controller/user/Rule.php +++ b/application/admin/controller/user/Rule.php @@ -18,6 +18,7 @@ class Rule extends Backend */ protected $model = null; protected $rulelist = []; + protected $multiFields = 'ismenu,status'; public function _initialize() { diff --git a/application/admin/lang/zh-cn.php b/application/admin/lang/zh-cn.php index aac6bf4f..1ccf0ba4 100755 --- a/application/admin/lang/zh-cn.php +++ b/application/admin/lang/zh-cn.php @@ -112,6 +112,7 @@ return [ 'Go back' => '返回首页', 'Jump now' => '立即跳转', 'Click to search %s' => '点击搜索 %s', + 'Click to toggle' => '点击切换', 'Operation completed' => '操作成功!', 'Operation failed' => '操作失败!', 'Unknown data format' => '未知的数据格式!', diff --git a/application/admin/library/Auth.php b/application/admin/library/Auth.php index c0836adb..0524a353 100644 --- a/application/admin/library/Auth.php +++ b/application/admin/library/Auth.php @@ -31,26 +31,23 @@ class Auth extends \fast\Auth /** * 管理员登录 * - * @param string $username 用户名 - * @param string $password 密码 - * @param int $keeptime 有效时长 + * @param string $username 用户名 + * @param string $password 密码 + * @param int $keeptime 有效时长 * @return boolean */ public function login($username, $password, $keeptime = 0) { $admin = Admin::get(['username' => $username]); - if (!$admin) - { + if (!$admin) { $this->setError('Username is incorrect'); return false; } - if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) - { + if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) { $this->setError('Please try again after 1 day'); return false; } - if ($admin->password != md5(md5($password) . $admin->salt)) - { + if ($admin->password != md5(md5($password) . $admin->salt)) { $admin->loginfailure++; $admin->save(); $this->setError('Password is incorrect'); @@ -71,8 +68,7 @@ class Auth extends \fast\Auth public function logout() { $admin = Admin::get(intval($this->id)); - if (!$admin) - { + if (!$admin) { return true; } $admin->token = ''; @@ -89,30 +85,24 @@ class Auth extends \fast\Auth public function autologin() { $keeplogin = Cookie::get('keeplogin'); - if (!$keeplogin) - { + if (!$keeplogin) { return false; } list($id, $keeptime, $expiretime, $key) = explode('|', $keeplogin); - if ($id && $keeptime && $expiretime && $key && $expiretime > time()) - { + if ($id && $keeptime && $expiretime && $key && $expiretime > time()) { $admin = Admin::get($id); - if (!$admin || !$admin->token) - { + if (!$admin || !$admin->token) { return false; } //token有变更 - if ($key != md5(md5($id) . md5($keeptime) . md5($expiretime) . $admin->token)) - { + if ($key != md5(md5($id) . md5($keeptime) . md5($expiretime) . $admin->token)) { return false; } Session::set("admin", $admin->toArray()); //刷新自动登录的时效 $this->keeplogin($keeptime); return true; - } - else - { + } else { return false; } } @@ -120,13 +110,12 @@ class Auth extends \fast\Auth /** * 刷新保持登录的Cookie * - * @param int $keeptime + * @param int $keeptime * @return boolean */ protected function keeplogin($keeptime = 0) { - if ($keeptime) - { + if ($keeptime) { $expiretime = time() + $keeptime; $key = md5(md5($this->id) . md5($keeptime) . md5($expiretime) . $this->token); $data = [$this->id, $keeptime, $expiretime, $key]; @@ -150,15 +139,13 @@ class Auth extends \fast\Auth { $request = Request::instance(); $arr = is_array($arr) ? $arr : explode(',', $arr); - if (!$arr) - { + if (!$arr) { return FALSE; } $arr = array_map('strtolower', $arr); // 是否存在 - if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) - { + if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) { return TRUE; } @@ -173,21 +160,17 @@ class Auth extends \fast\Auth */ public function isLogin() { - if ($this->logined) - { + if ($this->logined) { return true; } $admin = Session::get('admin'); - if (!$admin) - { + if (!$admin) { return false; } //判断是否同一时间同一账号只能在一个地方登录 - if (Config::get('fastadmin.login_unique')) - { + if (Config::get('fastadmin.login_unique')) { $my = Admin::get($admin['id']); - if (!$my || $my['token'] != $admin['token']) - { + if (!$my || $my['token'] != $admin['token']) { return false; } } @@ -252,9 +235,8 @@ class Auth extends \fast\Auth { $groups = $this->getGroups($uid); $groupIds = []; - foreach ($groups as $K => $v) - { - $groupIds[] = (int) $v['group_id']; + foreach ($groups as $K => $v) { + $groupIds[] = (int)$v['group_id']; } return $groupIds; } @@ -269,17 +251,14 @@ class Auth extends \fast\Auth //取出当前管理员所有的分组 $groups = $this->getGroups(); $groupIds = []; - foreach ($groups as $k => $v) - { + foreach ($groups as $k => $v) { $groupIds[] = $v['id']; } // 取出所有分组 $groupList = \app\admin\model\AuthGroup::where(['status' => 'normal'])->select(); $objList = []; - foreach ($groups as $K => $v) - { - if ($v['rules'] === '*') - { + foreach ($groups as $K => $v) { + if ($v['rules'] === '*') { $objList = $groupList; break; } @@ -289,12 +268,10 @@ class Auth extends \fast\Auth $objList = array_merge($objList, Tree::instance()->getTreeList($obj)); } $childrenGroupIds = []; - foreach ($objList as $k => $v) - { + foreach ($objList as $k => $v) { $childrenGroupIds[] = $v['id']; } - if (!$withself) - { + if (!$withself) { $childrenGroupIds = array_diff($childrenGroupIds, $groupIds); } return $childrenGroupIds; @@ -308,33 +285,25 @@ class Auth extends \fast\Auth public function getChildrenAdminIds($withself = false) { $childrenAdminIds = []; - if (!$this->isSuperAdmin()) - { + if (!$this->isSuperAdmin()) { $groupIds = $this->getChildrenGroupIds(false); $authGroupList = \app\admin\model\AuthGroupAccess:: - field('uid,group_id') - ->where('group_id', 'in', $groupIds) - ->select(); + field('uid,group_id') + ->where('group_id', 'in', $groupIds) + ->select(); - foreach ($authGroupList as $k => $v) - { + foreach ($authGroupList as $k => $v) { $childrenAdminIds[] = $v['uid']; } - } - else - { + } else { //超级管理员拥有所有人的权限 $childrenAdminIds = Admin::column('id'); } - if ($withself) - { - if (!in_array($this->id, $childrenAdminIds)) - { + if ($withself) { + if (!in_array($this->id, $childrenAdminIds)) { $childrenAdminIds[] = $this->id; } - } - else - { + } else { $childrenAdminIds = array_diff($childrenAdminIds, [$this->id]); } return $childrenAdminIds; @@ -350,15 +319,12 @@ class Auth extends \fast\Auth if ($this->breadcrumb || !$path) return $this->breadcrumb; $path_rule_id = 0; - foreach ($this->rules as $rule) - { + foreach ($this->rules as $rule) { $path_rule_id = $rule['name'] == $path ? $rule['id'] : $path_rule_id; } - if ($path_rule_id) - { + if ($path_rule_id) { $this->breadcrumb = Tree::instance()->init($this->rules)->getParents($path_rule_id, true); - foreach ($this->breadcrumb as $k => &$v) - { + foreach ($this->breadcrumb as $k => &$v) { $v['url'] = url($v['name']); $v['title'] = __($v['title']); } @@ -367,10 +333,11 @@ class Auth extends \fast\Auth } /** - * 获取左侧菜单栏 + * 获取左侧和顶部菜单栏 * * @param array $params URL对应的badge数据 - * @return string + * @param string $fixedPage 默认页 + * @return array */ public function getSidebar($params = [], $fixedPage = 'dashboard') { @@ -379,26 +346,21 @@ class Auth extends \fast\Auth $badgeList = []; $module = request()->module(); // 生成菜单的badge - foreach ($params as $k => $v) - { + foreach ($params as $k => $v) { $url = $k; - if (is_array($v)) - { + if (is_array($v)) { $nums = isset($v[0]) ? $v[0] : 0; $color = isset($v[1]) ? $v[1] : $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums]; $class = isset($v[2]) ? $v[2] : 'label'; - } - else - { + } else { $nums = $v; $color = $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums]; $class = 'label'; } //必须nums大于0才显示 - if ($nums) - { + if ($nums) { $badgeList[$url] = '' . $nums . ''; } } @@ -409,10 +371,8 @@ class Auth extends \fast\Auth $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader'); // 必须将结果集转换为数组 $ruleList = collection(\app\admin\model\AuthRule::where('status', 'normal')->where('ismenu', 1)->order('weigh', 'desc')->cache("__menu__")->select())->toArray(); - foreach ($ruleList as $k => &$v) - { - if (!in_array($v['name'], $userRule)) - { + foreach ($ruleList as $k => &$v) { + if (!in_array($v['name'], $userRule)) { unset($ruleList[$k]); continue; } @@ -423,16 +383,45 @@ class Auth extends \fast\Auth $v['pinyin'] = $pinyin->permalink($v['title'], ''); $v['title'] = __($v['title']); } - // 构造菜单数据 - Tree::instance()->init($ruleList); - $menu = Tree::instance()->getTreeMenu(0, '