Skip to content

Commit

Permalink
feat: enable minimum system protection
Browse files Browse the repository at this point in the history
  • Loading branch information
Trevor-Lan committed Aug 25, 2022
1 parent bfd4913 commit 1435295
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewDeleteConfigDictLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
}

func (l *DeleteConfigDictLogic) DeleteConfigDict(req *types.DeleteConfigDictReq) error {
if req.Id <= config.SysMaxDictionaryId {
if req.Id <= config.SysProtectDictionaryMaxId {
return errorx.NewDefaultError(errorx.ForbiddenErrorCode)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewDeleteSysPermMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}

func (l *DeleteSysPermMenuLogic) DeleteSysPermMenu(req *types.DeleteSysPermMenuReq) error {
if req.Id <= config.SysMaxPermMenuId {
if req.Id <= config.SysProtectPermMenuMaxId {
return errorx.NewDefaultError(errorx.ForbiddenErrorCode)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func NewUpdateSysPermMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}

func (l *UpdateSysPermMenuLogic) UpdateSysPermMenu(req *types.UpdateSysPermMenuReq) error {
if req.Id <= config.SysProtectPermMenuMaxId {
return errorx.NewDefaultError(errorx.ForbiddenErrorCode)
}

if req.Id == req.ParentId {
return errorx.NewDefaultError(errorx.ParentPermMenuErrorCode)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewDeleteSysRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Del
}

func (l *DeleteSysRoleLogic) DeleteSysRole(req *types.DeleteSysRoleReq) error {
if req.Id == config.SysSuperAdminRoleId {
if req.Id == config.SysProtectRoleId {
return errorx.NewDefaultError(errorx.ForbiddenErrorCode)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewUpdateSysRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Upd
}

func (l *UpdateSysRoleLogic) UpdateSysRole(req *types.UpdateSysRoleReq) error {
if req.Id == config.SysSuperAdminRoleId {
if req.Id == config.SysProtectRoleId {
return errorx.NewDefaultError(errorx.NotPermMenuErrorCode)
}

Expand Down
10 changes: 9 additions & 1 deletion app/core/cmd/api/internal/logic/sys/user/addsysuserlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"ark-admin-zero/app/core/cmd/api/internal/svc"
"ark-admin-zero/app/core/cmd/api/internal/types"
"ark-admin-zero/app/core/model"
"ark-admin-zero/common/config"
"ark-admin-zero/common/errorx"
"ark-admin-zero/common/utils"

Expand Down Expand Up @@ -40,7 +41,14 @@ func (l *AddSysUserLogic) AddSysUser(req *types.AddSysUserReq) error {
bytes, err := json.Marshal(req.RoleIds)
sysUser.RoleIds = string(bytes)
dictionary, err := l.svcCtx.SysDictionaryModel.FindOneByUniqueKey(l.ctx, "sys_pwd")
sysUser.Password = utils.MD5(dictionary.Value + l.svcCtx.Config.Salt)
var password string
if dictionary.Status == config.SysEnable {
password = dictionary.Value
} else {
password = config.SysNewUserDefaultPassword
}

sysUser.Password = utils.MD5(password + l.svcCtx.Config.Salt)
sysUser.Avatar = utils.AvatarUrl()
_, err = l.svcCtx.SysUserModel.Insert(l.ctx, sysUser)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewDeleteSysUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Del
}

func (l *DeleteSysUserLogic) DeleteSysUser(req *types.DeleteSysUserReq) error {
if req.Id == config.SysSuperAdminUserId {
if req.Id == config.SysProtectUserId {
return errorx.NewDefaultError(errorx.ForbiddenErrorCode)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (l *GetUserPermMenuLogic) GetUserPermMenu() (resp *types.UserPermMenuResp,
}

func (l *GetUserPermMenuLogic) countUserPermMenu(roles []uint64, permMenu []uint64) ([]*model.SysPermMenu, []uint64, error) {
if utils.ArrayContainValue(roles, config.SysSuperAdminRoleId) {
if utils.ArrayContainValue(roles, config.SysProtectRoleId) {
sysPermMenus, err := l.svcCtx.SysPermMenuModel.FindAll(l.ctx)
if err != nil {
return nil, permMenu, err
Expand Down
2 changes: 1 addition & 1 deletion app/core/cmd/api/internal/logic/user/loginlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (l *LoginLogic) Login(req *types.LoginReq, r *http.Request) (resp *types.Lo
return nil, errorx.NewDefaultError(errorx.AccountDisableErrorCode)
}

if sysUser.Id != config.SysSuperAdminUserId {
if sysUser.Id != config.SysProtectUserId {
dept, _ := l.svcCtx.SysDeptModel.FindOne(l.ctx, sysUser.DeptId)
if dept.Status == 0 {
return nil, errorx.NewDefaultError(errorx.AccountDisableErrorCode)
Expand Down
4 changes: 2 additions & 2 deletions app/core/model/sysrolemodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewSysRoleModel(conn sqlx.SqlConn, c cache.CacheConf) SysRoleModel {
}

func (m *customSysRoleModel) FindAll(ctx context.Context) ([]*SysRole, error) {
query := fmt.Sprintf("SELECT %s FROM %s WHERE id!=%d ORDER BY order_num DESC", sysRoleRows, m.table, config.SysSuperAdminRoleId)
query := fmt.Sprintf("SELECT %s FROM %s WHERE id!=%d ORDER BY order_num DESC", sysRoleRows, m.table, config.SysProtectRoleId)
var resp []*SysRole
err := m.QueryRowsNoCacheCtx(ctx, &resp, query)
switch err {
Expand All @@ -45,7 +45,7 @@ func (m *customSysRoleModel) FindAll(ctx context.Context) ([]*SysRole, error) {
}

func (m *customSysRoleModel) FindEnable(ctx context.Context) ([]*SysRole, error) {
query := fmt.Sprintf("SELECT %s FROM %s WHERE id!=%d AND status=1 ORDER BY order_num DESC", sysRoleRows, m.table, config.SysSuperAdminRoleId)
query := fmt.Sprintf("SELECT %s FROM %s WHERE id!=%d AND status=1 ORDER BY order_num DESC", sysRoleRows, m.table, config.SysProtectRoleId)
var resp []*SysRole
err := m.QueryRowsNoCacheCtx(ctx, &resp, query)
switch err {
Expand Down
4 changes: 2 additions & 2 deletions app/core/model/sysusermodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewSysUserModel(conn sqlx.SqlConn, c cache.CacheConf) SysUserModel {

func (m *customSysUserModel) FindPage(ctx context.Context, page uint64, limit uint64, deptIds string) ([]*SysUserDetail, error) {
offset := (page - 1) * limit
query := fmt.Sprintf("SELECT u.id,u.dept_id,u.job_id,u.profession_id,u.account,u.username,u.nickname,u.avatar,u.gender,IFNULL(p.name,'NULL') as profession,IFNULL(j.name,'NULL') as job,IFNULL(d.name,'NULL') as dept,IFNULL(GROUP_CONCAT(r.name),'NULL') as roles,IFNULL(GROUP_CONCAT(r.id),0) as role_ids,u.email,u.mobile,u.remark,u.order_num,u.status,u.create_time,u.update_time FROM (SELECT * FROM sys_user WHERE id!=%d AND dept_id IN(%s) ORDER BY order_num DESC LIMIT %d,%d) u LEFT JOIN sys_profession p ON u.profession_id=p.id LEFT JOIN sys_dept d ON u.dept_id=d.id LEFT JOIN sys_job j ON u.job_id=j.id LEFT JOIN sys_role r ON JSON_CONTAINS(u.role_ids,JSON_ARRAY(r.id)) GROUP BY u.id", config.SysSuperAdminUserId, deptIds, offset, limit)
query := fmt.Sprintf("SELECT u.id,u.dept_id,u.job_id,u.profession_id,u.account,u.username,u.nickname,u.avatar,u.gender,IFNULL(p.name,'NULL') as profession,IFNULL(j.name,'NULL') as job,IFNULL(d.name,'NULL') as dept,IFNULL(GROUP_CONCAT(r.name),'NULL') as roles,IFNULL(GROUP_CONCAT(r.id),0) as role_ids,u.email,u.mobile,u.remark,u.order_num,u.status,u.create_time,u.update_time FROM (SELECT * FROM sys_user WHERE id!=%d AND dept_id IN(%s) ORDER BY order_num DESC LIMIT %d,%d) u LEFT JOIN sys_profession p ON u.profession_id=p.id LEFT JOIN sys_dept d ON u.dept_id=d.id LEFT JOIN sys_job j ON u.job_id=j.id LEFT JOIN sys_role r ON JSON_CONTAINS(u.role_ids,JSON_ARRAY(r.id)) GROUP BY u.id", config.SysProtectUserId, deptIds, offset, limit)
var resp []*SysUserDetail
err := m.QueryRowsNoCacheCtx(ctx, &resp, query)
switch err {
Expand All @@ -88,7 +88,7 @@ func (m *customSysUserModel) FindCountByCondition(ctx context.Context, condition
}

func (m *customSysUserModel) FindCountByDeptIds(ctx context.Context, deptIds string) (uint64, error) {
query := fmt.Sprintf("SELECT COUNT(id) FROM %s WHERE id!=%d AND dept_id IN(%s)", m.table, config.SysSuperAdminUserId, deptIds)
query := fmt.Sprintf("SELECT COUNT(id) FROM %s WHERE id!=%d AND dept_id IN(%s)", m.table, config.SysProtectUserId, deptIds)
var resp uint64
err := m.QueryRowNoCacheCtx(ctx, &resp, query)
switch err {
Expand Down
13 changes: 7 additions & 6 deletions common/config/system.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package config

const (
SysPermMenuPrefix = "/"
SysJwtUserId = "userId"
SysPermMenuCachePrefix = "cache:arkAdmin:permMenu:"
SysOnlineUserCachePrefix = "cache:arkAdmin:online:"
SysLoginCaptchaCachePrefix = "cache:arkAdmin:captcha:"
SysUserIdCachePrefix = "cache:arkAdmin:sysUser:id:"
SysSuperAdminUserId = 1
SysSuperAdminRoleId = 1
SysPermMenuPrefix = "/"
SysDateFormat = "2006.01.02 15:04:05"
SysNewUserDefaultPassword = "123456"
SysProtectPermMenuMaxId = 44
SysProtectDictionaryMaxId = 4
SysProtectUserId = 1
SysProtectRoleId = 1
SysDefaultPermType = 2
SysMaxPermMenuId = 50
SysMaxDictionaryId = 4
SysEnable = 1
SysDisable = 0
SysTopMenuId = 0
SysLoginLogType = 1
SysDateFormat = "2006.01.02 15:04:05"
)
19 changes: 9 additions & 10 deletions doc/sql/ark_admin.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/
--
-- 主机: mysql
-- 生成日期: 2022-08-25 03:34:21
-- 生成日期: 2022-08-25 07:03:50
-- 服务器版本: 5.7.36
-- PHP 版本: 7.4.27

Expand Down Expand Up @@ -47,7 +47,7 @@ CREATE TABLE `sys_dept` (

INSERT INTO `sys_dept` (`id`, `parent_id`, `name`, `full_name`, `unique_key`, `type`, `status`, `order_num`, `remark`, `create_time`, `update_time`) VALUES
(1, 0, '方舟', '方舟互联', 'arklnk', 1, 1, 0, '', '2022-08-17 02:09:17', '2022-08-22 02:13:54'),
(2, 0, '思忆', '思忆技术', 'siyee', 1, 1, 0, '', '2022-08-19 06:40:10', '2022-08-22 02:13:39'),
(2, 0, '思忆', '思忆技术', 'siyee', 1, 1, 0, '', '2022-08-19 06:40:10', '2022-08-25 06:54:29'),
(3, 0, '演示', '演示部门', 'demo', 1, 1, 0, '', '2022-08-23 14:02:27', '2022-08-25 02:27:51');

-- --------------------------------------------------------
Expand Down Expand Up @@ -77,8 +77,8 @@ CREATE TABLE `sys_dictionary` (
INSERT INTO `sys_dictionary` (`id`, `parent_id`, `name`, `type`, `unique_key`, `value`, `status`, `order_num`, `remark`, `create_time`, `update_time`) VALUES
(1, 0, '系统配置', 0, 'sys', '', 1, 0, '', '2022-08-22 10:03:58', '2022-08-23 01:25:31'),
(2, 1, '默认密码', 1, 'sys_pwd', '123456', 1, 0, '新建用户默认密码', '2022-08-22 10:03:58', '2022-08-24 05:28:06'),
(3, 1, '更新密码', 1, 'sys_ch_pwd', '', 0, 0, '', '2022-08-25 03:18:47', '2022-08-25 03:29:24'),
(4, 1, '更新个人资料', 1, 'sys_userinfo', '', 0, 0, '', '2022-08-25 03:28:36', '2022-08-25 03:33:18');
(3, 1, '更新个人密码', 1, 'sys_ch_pwd', '', 0, 0, '', '2022-08-25 03:18:47', '2022-08-25 04:47:01'),
(4, 1, '更新个人资料', 1, 'sys_userinfo', '', 0, 0, '', '2022-08-25 03:28:36', '2022-08-25 04:41:07');

-- --------------------------------------------------------

Expand Down Expand Up @@ -181,7 +181,7 @@ INSERT INTO `sys_perm_menu` (`id`, `parent_id`, `name`, `router`, `perms`, `type
(31, 29, '新增', '', '[\"sys/user/add\",\"sys/user/rdpj/info\"]', 2, '', 0, '', 1, '', '2022-08-12 02:14:20', '2022-08-24 03:17:19'),
(32, 29, '删除', '', '[\"sys/user/delete\"]', 2, '', 0, '', 1, '', '2022-08-12 02:14:20', '2022-08-23 09:37:20'),
(33, 29, '更新', '', '[\"sys/user/update\",\"sys/user/rdpj/info\"]', 2, '', 0, '', 1, '', '2022-08-12 02:14:20', '2022-08-24 03:08:07'),
(34, 29, '更改密码', '', '[\"sys/user/password/update\"]', 2, '', 0, '', 1, '', '2022-08-12 02:14:20', '2022-08-23 09:34:28'),
(34, 29, '改密', '', '[\"sys/user/password/update\"]', 2, '', 0, '', 1, '', '2022-08-12 02:14:20', '2022-08-25 04:51:46'),
(35, 29, '转移', '', '[\"sys/user/transfer\"]', 2, '', 0, '', 1, '', '2022-08-12 02:14:20', '2022-08-23 09:34:36'),
(36, 0, '配置管理', '/config', '[]', 0, 'config', 0, '', 1, '', '2022-08-22 03:33:42', '2022-08-24 03:41:35'),
(37, 36, '字典管理', '/config/dict', '[]', 1, '', 0, 'views/config/dict', 1, '', '2022-08-22 03:39:21', '2022-08-23 09:33:47'),
Expand All @@ -191,8 +191,7 @@ INSERT INTO `sys_perm_menu` (`id`, `parent_id`, `name`, `router`, `perms`, `type
(41, 37, '更新', '', '[\"config/dict/update\"]', 2, '', 0, '', 1, '', '2022-08-22 03:42:07', '2022-08-23 09:37:34'),
(42, 0, '日志管理', '/log', '[]', 0, 'log', 0, '', 1, '', '2022-08-23 04:47:23', '2022-08-24 03:52:57'),
(43, 42, '登录日志', '/log/login', '[]', 1, '', 0, 'views/log/login', 1, '', '2022-08-23 04:47:51', '2022-08-23 09:42:43'),
(44, 43, '查询', '', '[\"log/login/page\"]', 2, '', 0, '', 1, '', '2022-08-22 03:42:07', '2022-08-23 09:35:53'),
(45, 43, '删除', '', '[\"log/login/delete\"]', 2, '', 0, '', 1, '', '2022-08-22 03:42:07', '2022-08-23 09:37:20');
(44, 43, '查询', '', '[\"log/login/page\"]', 2, '', 0, '', 1, '', '2022-08-22 03:42:07', '2022-08-23 09:35:53');

-- --------------------------------------------------------

Expand Down Expand Up @@ -278,8 +277,8 @@ CREATE TABLE `sys_user` (
--

INSERT INTO `sys_user` (`id`, `account`, `password`, `username`, `nickname`, `avatar`, `gender`, `email`, `mobile`, `profession_id`, `job_id`, `dept_id`, `role_ids`, `status`, `order_num`, `remark`, `create_time`, `update_time`) VALUES
(1, 'arklnk', '596bfe4bb02db60c2a25965598529e7e', 'arklnk', 'arklnk', 'https://avataaars.io/?clotheColor=PastelBlue&accessoriesType=Sunglasses&avatarStyle=Circle&clotheType=GraphicShirt&eyeType=Dizzy&eyebrowType=UnibrowNatural&facialHairColor=Platinum&facialHairType=BeardLight&hairColor=Brown&hatColor=Heather&mouthType=Grimace&skinColor=DarkBrown&topType=ShortHairDreads01', 0, 'arklnk@163.com', '12000000000', 0, 0, 0, '[1]', 1, 0, 'arklnk', '2022-08-11 06:19:45', '2022-08-25 03:09:01'),
(2, 'demo', '596bfe4bb02db60c2a25965598529e7e', 'demo', '', 'https://avataaars.io/?avatarStyle=Circle&topType=Hat&accessoriesType=Sunglasses&facialHairType=Blank&clotheType=Hoodie&clotheColor=Heather&eyeType=Hearts&eyebrowType=UpDown&mouthType=Tongue&skinColor=DarkBrown', 0, '', '', 3, 4, 3, '[2]', 1, 0, '', '2022-08-23 14:04:24', '2022-08-24 08:44:59');
(1, 'arklnk', '596bfe4bb02db60c2a25965598529e7e', 'arklnk', 'arklnk', 'https://avataaars.io/?clotheColor=Black&accessoriesType=Wayfarers&avatarStyle=Circle&clotheType=Hoodie&eyeType=Dizzy&eyebrowType=RaisedExcitedNatural&facialHairColor=Brown&facialHairType=BeardMedium&hairColor=Auburn&hatColor=Blue03&mouthType=Grimace&skinColor=Tanned&topType=LongHairFro', 0, 'arklnk@163.com', '12000000000', 0, 0, 0, '[1]', 1, 0, 'arklnk', '2022-08-11 06:19:45', '2022-08-25 04:40:45'),
(2, 'demo', '596bfe4bb02db60c2a25965598529e7e', 'demo', '', 'https://avataaars.io/?avatarStyle=Circle&topType=Hat&accessoriesType=Sunglasses&facialHairType=Blank&clotheType=Hoodie&clotheColor=Heather&eyeType=Hearts&eyebrowType=UpDown&mouthType=Tongue&skinColor=DarkBrown', 0, '', '', 3, 4, 3, '[2]', 1, 0, '', '2022-08-23 14:04:24', '2022-08-25 05:00:57');

--
-- 转储表的索引
Expand Down Expand Up @@ -371,7 +370,7 @@ ALTER TABLE `sys_log`
-- 使用表AUTO_INCREMENT `sys_perm_menu`
--
ALTER TABLE `sys_perm_menu`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '编号', AUTO_INCREMENT=46;
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '编号', AUTO_INCREMENT=45;

--
-- 使用表AUTO_INCREMENT `sys_profession`
Expand Down

0 comments on commit 1435295

Please sign in to comment.