Example #1
0
        public async Task Create(CreateUserGroupInput input)
        {
            var userGroup = input.MapTo <UserGroup>();
            var thisLevelUserGroupCount = await _userGroupRepository.GetCountAsync(p => p.ParentId == input.ParentId);

            if (input.ParentId == 0)
            {
                userGroup.Level = 1;
                userGroup.Code  = (thisLevelUserGroupCount + 1).ToString().PadRight(HeroConstants.CodeRuleRestrain.CodeCoverBit, HeroConstants.CodeRuleRestrain.CodeCoverSymbol);
            }
            else
            {
                var parentUserGroup = await _userGroupRepository.SingleOrDefaultAsync(p => p.Id == input.ParentId);

                if (parentUserGroup == null)
                {
                    throw new BusinessException($"不存在Id为{input.ParentId}的用户组信息");
                }
                userGroup.Level = parentUserGroup.Level + 1;
                userGroup.Code  = parentUserGroup.Code + HeroConstants.CodeRuleRestrain.CodeSeparator + (thisLevelUserGroupCount + 1).ToString().PadLeft(HeroConstants.CodeRuleRestrain.CodeCoverBit, HeroConstants.CodeRuleRestrain.CodeCoverSymbol);
            }
            await UnitOfWorkAsync(async (conn, trans) =>
            {
                var userGroupId = await _userGroupRepository.InsertAndGetIdAsync(userGroup, conn, trans);
                foreach (var userId in input.UserIds)
                {
                    var userInfo = await _userRepository.SingleOrDefaultAsync(p => p.Id == userId);
                    if (userInfo == null)
                    {
                        throw new BusinessException($"不存在用户Id为{userId}的用户信息");
                    }
                    await _userUserGroupRelationRepository.InsertAsync(new UserUserGroupRelation()
                    {
                        UserGroupId = userGroupId, UserId = userId
                    }, conn, trans);
                }
                foreach (var roleId in input.RoleIds)
                {
                    var roleInfo = await _roleRepository.SingleOrDefaultAsync(p => p.Id == roleId);
                    if (roleInfo == null)
                    {
                        throw new BusinessException($"不存在用户Id为{roleId}的角色信息");
                    }
                    await _userGroupRoleRepository.InsertAsync(new UserGroupRole()
                    {
                        UserGroupId = userGroupId, RoleId = roleId
                    }, conn, trans);
                }
            }, Connection);
        }
Example #2
0
        public async Task <string> Create(CreateUserGroupInput input)
        {
            input.CheckDataAnnotations().CheckValidResult();
            if (input.ParentId != 0)
            {
                var parentUserGroup = await _userGroupRepository.SingleOrDefaultAsync(p => p.Id == input.ParentId);

                if (parentUserGroup == null)
                {
                    throw new BusinessException($"不存在父Id为{input.ParentId}的用户组");
                }
            }
            var existUserGroup = await _userGroupRepository.FirstOrDefaultAsync(p => p.GroupCode == input.GroupCode);

            if (existUserGroup != null)
            {
                throw new BusinessException($"已经存在{input.GroupCode}的用户组");
            }
            var userGroupEntity = input.MapTo <UserGroup>();
            await _userGroupRepository.InsertAsync(userGroupEntity);

            return("新增用户组成功");
        }
Example #3
0
        public async Task Create(CreateUserGroupInput input)
        {
            CheckUserDefinedDataPermission(input.DataPermissionType, input.DataPermissionOrgIds);
            using (var locker = await _lockerProvider.CreateLockAsync("CreateUserGroup"))
            {
                await locker.Lock(async() =>
                {
                    var exsitUserGroup = await _userGroupRepository.FirstOrDefaultAsync(p => p.Identification == input.Identification, false);
                    if (exsitUserGroup != null)
                    {
                        throw new BusinessException($"系统中已经存在{input.Identification}的用户组");
                    }
                    var userGroup = input.MapTo <UserGroup>();
                    await UnitOfWorkAsync(async(conn, trans) =>
                    {
                        var userGroupId = await _userGroupRepository.InsertAndGetIdAsync(userGroup, conn, trans);
                        foreach (var roleId in input.RoleIds)
                        {
                            await _userGroupRoleRepository.InsertAsync(
                                new UserGroupRole {
                                UserGroupId = userGroupId, RoleId = roleId
                            }, conn, trans);
                        }

                        if (!input.IsAllOrg)
                        {
                            foreach (var orgId in input.OrgIds)
                            {
                                var userGroupOrg = new UserGroupOrganization()
                                {
                                    UserGroupId = userGroupId, OrgId = orgId
                                };
                                await _userGroupOrganizationRepository.InsertAsync(userGroupOrg, conn, trans);
                            }
                        }
                        var insertSql =
                            "INSERT INTO UserGroupPermission(PermissionId,UserGroupId,CreateTime,CreateBy,TenantId) VALUES(@PermissionId,@UserGroupId,@CreationTime,@CreatorUserId,@TenantId)";
                        var userGroupPermissions = new List <UserGroupPermission>();
                        foreach (var permissionId in input.PermissionIds)
                        {
                            userGroupPermissions.Add(new UserGroupPermission
                            {
                                PermissionId  = permissionId,
                                UserGroupId   = userGroupId,
                                CreationTime  = DateTime.Now,
                                CreatorUserId = _session.UserId,
                                TenantId      = _session.TenantId
                            });
                        }
                        await conn.ExecuteAsync(insertSql, userGroupPermissions, trans);
                        if (input.DataPermissionType == DataPermissionType.UserDefined)
                        {
                            var insertDataPermissionOrgSql =
                                "INSERT INTO UserGroupDataPermissionOrgRelation(UserGroupId,OrgId,CreateTime,CreateBy,TenantId) VALUES(@UserGroupId,@OrgId,@CreationTime,@CreatorUserId,@TenantId)";
                            var dataPermissionOrgDatas = new List <UserGroupDataPermissionOrgRelation>();
                            foreach (var orgId in input.DataPermissionOrgIds)
                            {
                                dataPermissionOrgDatas.Add(new UserGroupDataPermissionOrgRelation()
                                {
                                    UserGroupId   = userGroupId,
                                    OrgId         = orgId,
                                    CreationTime  = DateTime.Now,
                                    CreatorUserId = _session.UserId,
                                    TenantId      = _session.TenantId
                                });
                            }
                            await conn.ExecuteAsync(insertDataPermissionOrgSql, dataPermissionOrgDatas, trans);
                        }
                    }, Connection);
                });
            }
        }