Example #1
0
        public void UpdateRolesForAccount(string accountId, string[] roleIds, string creater)
        {
            using (MiniSenDbContext dbContext = new MiniSenDbContext())
            {
                CommonService <AdminUser> commonService = new CommonService <AdminUser>(dbContext);

                bool isExist = commonService.AnyByIdNoMarkDeleted(accountId);
                if (!isExist)
                {
                    throw new PushToUserException("Current account item is not exist");
                }

                try
                {
                    dbContext.DBTransaction.Begin();

                    dbContext.Update <AdminUser_Role>(ar => new { ar.DelFlag }, new AdminUser_Role {
                        DelFlag = 1
                    })
                    .Where(ar => ar.AdminUserId.Equals(accountId)).Done();

                    foreach (string roleId in roleIds)
                    {
                        isExist = dbContext.Queryable <Role>().Where(r => r.DelFlag == 0 && r.Id == roleId).Any();
                        if (!isExist)
                        {
                            throw new PushToUserException("the role you choosed is not exist");
                        }

                        dbContext.Add(new AdminUser_Role
                        {
                            Id          = Utils.GetGuidStr(),
                            AdminUserId = accountId,
                            RoleId      = roleId,
                            Creater     = creater,
                            Updater     = creater
                        });
                    }

                    dbContext.DBTransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbContext.DBTransaction.Rollback();

                    throw ex;
                }
            }
        }
Example #2
0
        public void EditMenuForRole(string roleId, string[] hasMenuIds, string updater)
        {
            using (MiniSenDbContext dbContext = new MiniSenDbContext())
            {
                CommonService <Role> commonService = new CommonService <Role>(dbContext);

                bool isExist = commonService.AnyByIdNoMarkDeleted(roleId);
                if (!isExist)
                {
                    throw new PushToUserException("Current role item is not exist");
                }

                try
                {
                    dbContext.DBTransaction.Begin();

                    dbContext.ExecuteSql(RoleService_SQL.MarkDeleteAllMenuPermsOfRole, roleId);

                    foreach (string menuId in hasMenuIds)
                    {
                        var permItem = dbContext.Queryable <Permission>().Select(p => new { p.Id }).Where(p => p.DelFlag == 0 && p.MenuId == menuId).ToOne();

                        if (null == permItem)
                        {
                            throw new PushToUserException("menu item is not exist");
                        }

                        dbContext.Add(new Role_Permission
                        {
                            Id           = Utils.GetGuidStr(),
                            RoleId       = roleId,
                            PermissionId = permItem.Id,
                            Creater      = updater,
                            Updater      = updater
                        });
                    }

                    dbContext.DBTransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbContext.DBTransaction.Rollback();

                    throw ex;
                }
            }
        }
Example #3
0
        public void EditApiPermForRole(string roleId, string[] hasApiPermissionIds, string updater)
        {
            using (MiniSenDbContext dbContext = new MiniSenDbContext())
            {
                CommonService <Role> commonService = new CommonService <Role>(dbContext);

                bool isExist = commonService.AnyByIdNoMarkDeleted(roleId);
                if (!isExist)
                {
                    throw new PushToUserException("Current role item is not exist");
                }

                try
                {
                    dbContext.DBTransaction.Begin();

                    dbContext.ExecuteSql(RoleService_SQL.MarkDeleteAllApiPermsOfRole, roleId);

                    foreach (string apiPermissionId in hasApiPermissionIds)
                    {
                        dbContext.Add(new Role_Permission
                        {
                            Id           = Utils.GetGuidStr(),
                            RoleId       = roleId,
                            PermissionId = apiPermissionId,
                            Creater      = updater,
                            Updater      = updater
                        });
                    }

                    dbContext.DBTransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbContext.DBTransaction.Rollback();

                    throw ex;
                }
            }
        }
Example #4
0
        public string AddNewOne(Menu_AddEditDTO addOne, string creater)
        {
            using (MiniSenDbContext dbContext = new MiniSenDbContext())
            {
                CommonService <Menu> commonService = new CommonService <Menu>(dbContext);

                bool hasExist = commonService.WhereNoMarkDeleted().Where(d => 0 == d.DelFlag && d.Name.Equals(addOne.Name)).Any();
                if (hasExist)
                {
                    throw new PushToUserException($"A menu item with the same name '{addOne.Name}' already exists");
                }

                if (!string.IsNullOrEmpty(addOne.PagePath))
                {
                    hasExist = commonService.WhereNoMarkDeleted().Where(d => d.PagePath.Equals(addOne.PagePath)).Any();
                    if (hasExist)
                    {
                        throw new PushToUserException($"A menu item with the same page path '{addOne.PagePath}' already exists");
                    }
                }

                try
                {
                    dbContext.DBTransaction.Begin();

                    Menu newOne = CoffeeMapper <Menu_AddEditDTO, Menu> .AutoMap(addOne, (_out, _in) =>
                    {
                        _out.Id      = Utils.GetGuidStr();
                        _out.Creater = creater;
                    });

                    string newMenuId = commonService.Insert(newOne);

                    //菜单项添加对应的权限项记录
                    if (2 == addOne.Type)
                    {
                        Permission menuPerm = new Permission
                        {
                            Id             = Utils.GetGuidStr(),
                            Name           = $"menu-({addOne.Name})",
                            PermissionType = 2,
                            MenuId         = newMenuId,
                            DelFlag        = 0,
                            Creater        = creater
                        };

                        dbContext.Add(menuPerm);
                    }

                    dbContext.DBTransaction.Commit();

                    return(newMenuId);
                }
                catch (Exception ex)
                {
                    dbContext.DBTransaction.Rollback();

                    throw ex;
                }
            }
        }