diff --git a/application/admin/controller/User.php b/application/admin/controller/User.php index b567161..5fdcf31 100644 --- a/application/admin/controller/User.php +++ b/application/admin/controller/User.php @@ -36,7 +36,7 @@ class User extends Base if ($err = $validate->getErrors($data)) { $this->error($err); } - $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT); + $data['password'] = generatePassword($data['password']); if ($userId = $userModel->store($data)) { // 分配角色 $this->giveRoles($userModel, $userId, $data); @@ -63,7 +63,7 @@ class User extends Base $this->error($err); } $this->giveRoles($userModel, $data['id'], $data); - $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT); + $data['password'] = generatePassword($data['password']); $userModel->updateBy($data['id'], $data) ? $this->success('修改成功', url('user/index')) : $this->error('修改失败'); } diff --git a/application/common.php b/application/common.php index 71ad321..4c0eca6 100644 --- a/application/common.php +++ b/application/common.php @@ -56,3 +56,12 @@ if (!function_exists('searchButton')) { return sprintf('', $name); } } + +/** + * 生成密码 + */ +if (!function_exists('generatePassword')) { + function generatePassword(string $password, int $algo = PASSWORD_DEFAULT) { + return password_hash($password, $algo); + } +} diff --git a/database/migrations/20181112081014_users.php b/database/migrations/20181112081014_users.php index b7dab8f..e081185 100644 --- a/database/migrations/20181112081014_users.php +++ b/database/migrations/20181112081014_users.php @@ -36,7 +36,7 @@ class Users extends Migrator ->addColumn('login_ip', 'string',['limit' => 50, 'default'=>'','comment'=>'登录IP']) ->addColumn('created_at', 'timestamp', [ 'comment' => '更新时间']) ->addColumn('login_at', 'timestamp', [ 'comment' => '最近登录时间']) - ->addIndex(['name'], ['unique' => true]) + ->addIndex(['name', 'email'], ['unique' => true]) ->create(); } } diff --git a/database/seeds/Permissions.php b/database/seeds/Permissions.php new file mode 100644 index 0000000..444bd9b --- /dev/null +++ b/database/seeds/Permissions.php @@ -0,0 +1,200 @@ + 1, + 'name' => '权限管理', + 'icon' => '', + 'pid' => 0, + 'module' => '', + 'controller' => '', + 'action' => '', + 'is_show' => 1, + ], + + [ + 'id' => 2, + 'name' => '用户管理', + 'icon' => '', + 'pid' => 1, + 'module' => 'admin', + 'controller' => 'user', + 'action' => 'index', + 'is_show' => 1, + ], + + [ + 'id' => 3, + 'name' => '角色管理', + 'icon' => '', + 'pid' => 1, + 'module' => 'admin', + 'controller' => 'role', + 'action' => 'index', + 'is_show' => 1, + ], + + [ + 'id' => 4, + 'name' => '菜单管理', + 'icon' => '', + 'pid' => 1, + 'module' => 'admin', + 'controller' => 'permission', + 'action' => 'index', + 'is_show' => 1, + ], + + [ + 'id' => 5, + 'name' => '创建用户', + 'icon' => '', + 'pid' => 2, + 'module' => 'admin', + 'controller' => 'user', + 'action' => 'create', + 'is_show' => 2, + ], + + [ + 'id' => 6, + 'name' => '编辑用户', + 'icon' => '', + 'pid' => 2, + 'module' => 'admin', + 'controller' => 'user', + 'action' => 'edit', + 'is_show' => 2, + ], + + [ + 'id' => 7, + 'name' => '删除用户', + 'icon' => '', + 'pid' => 2, + 'module' => 'admin', + 'controller' => 'user', + 'action' => 'delete', + 'is_show' => 2, + ], + + [ + 'id' => 8, + 'name' => '创建角色', + 'icon' => '', + 'pid' => 3, + 'module' => 'admin', + 'controller' => 'role', + 'action' => 'create', + 'is_show' => 2, + ], + + [ + 'id' => 9, + 'name' => '编辑角色', + 'icon' => '', + 'pid' => 3, + 'module' => 'admin', + 'controller' => 'role', + 'action' => 'edit', + 'is_show' => 2, + ], + + [ + 'id' => 10, + 'name' => '删除角色', + 'icon' => '', + 'pid' => 3, + 'module' => 'admin', + 'controller' => 'role', + 'action' => 'delete', + 'is_show' => 2, + ], + + [ + 'id' => 11, + 'name' => '获取角色权限', + 'icon' => '', + 'pid' => 3, + 'module' => 'admin', + 'controller' => 'role', + 'action' => 'getPermissionsOfRole', + 'is_show' => 2, + ], + + [ + 'id' => 12, + 'name' => '分配权限', + 'icon' => '', + 'pid' => 3, + 'module' => 'admin', + 'controller' => 'role', + 'action' => 'givePermissions', + 'is_show' => 2, + ], + + [ + 'id' => 13, + 'name' => '分配角色', + 'icon' => '', + 'pid' => 2, + 'module' => 'admin', + 'controller' => 'user', + 'action' => 'giveRoles', + 'is_show' => 2, + ], + + [ + 'id' => 14, + 'name' => '创建菜单', + 'icon' => '', + 'pid' => 4, + 'module' => 'admin', + 'controller' => 'permission', + 'action' => 'create', + 'is_show' => 2, + ], + + [ + 'id' => 15, + 'name' => '编辑菜单', + 'icon' => '', + 'pid' => 4, + 'module' => 'admin', + 'controller' => 'permission', + 'action' => 'edit', + 'is_show' => 2, + ], + + [ + 'id' => 16, + 'name' => '删除菜单', + 'icon' => '', + 'pid' => 4, + 'module' => 'admin', + 'controller' => 'permission', + 'action' => 'delete', + 'is_show' => 2, + ], + + ]; + + $this->table(config('permissions.table.permission'))->insert($data)->save(); + + + } +} \ No newline at end of file diff --git a/database/seeds/RoleHasPermissions.php b/database/seeds/RoleHasPermissions.php new file mode 100644 index 0000000..c40db0c --- /dev/null +++ b/database/seeds/RoleHasPermissions.php @@ -0,0 +1,28 @@ + 1, + 'permission_id' => $v->id, + ]; + } + + $this->table(config('permissions.table.role_has_permissions'))->insert($data)->save(); + } +} \ No newline at end of file diff --git a/database/seeds/Roles.php b/database/seeds/Roles.php new file mode 100644 index 0000000..741eb3e --- /dev/null +++ b/database/seeds/Roles.php @@ -0,0 +1,24 @@ + '超级管理员', + ]; + + $this->table(config('permissions.table.role'))->insert($data)->save(); + } +} \ No newline at end of file diff --git a/database/seeds/UserHasRoles.php b/database/seeds/UserHasRoles.php new file mode 100644 index 0000000..8ad7749 --- /dev/null +++ b/database/seeds/UserHasRoles.php @@ -0,0 +1,24 @@ + 1, + 'role_id' => 1, + ]; + + $this->table(config('permissions.table.user_has_roles'))->insert($data)->save(); + } +} \ No newline at end of file diff --git a/database/seeds/Users.php b/database/seeds/Users.php new file mode 100644 index 0000000..a7ee624 --- /dev/null +++ b/database/seeds/Users.php @@ -0,0 +1,25 @@ + 'admin', + 'email' => 'admin@gmail.com', + 'password' => password_hash('admin', PASSWORD_DEFAULT), + ]; + + $this->table('users')->insert([$data])->save(); + } +} \ No newline at end of file