예제 #1
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);
                });
            }
        }
예제 #2
0
        public async Task Update(UpdateUserGroupInput input)
        {
            CheckUserDefinedDataPermission(input.DataPermissionType, input.DataPermissionOrgIds);
            using (var locker = await _lockerProvider.CreateLockAsync("UpdateUserGroup"))
            {
                await locker.Lock(async() =>
                {
                    var userGroup = await _userGroupRepository.SingleOrDefaultAsync(p => p.Id == input.Id);
                    if (userGroup == null)
                    {
                        throw new BusinessException($"不存在Id为{input.Id}的用户组");
                    }
                    if (!userGroup.Identification.Equals(input.Identification))
                    {
                        var exsitUserGroup = await _userGroupRepository.FirstOrDefaultAsync(p => p.Identification == input.Identification, false);
                        if (exsitUserGroup != null)
                        {
                            throw new BusinessException($"系统中已经存在{input.Identification}的用户组");
                        }
                    }
                    userGroup = input.MapTo(userGroup);
                    await UnitOfWorkAsync(async(conn, trans) =>
                    {
                        var userGroupOrganizationIds =
                            (await _userGroupOrganizationRepository.GetAllAsync(p => p.UserGroupId == userGroup.Id, conn,
                                                                                trans)).Select(p => p.OrgId).ToArray();
                        await _userGroupRepository.UpdateAsync(userGroup, conn, trans);
                        await _userGroupRoleRepository.DeleteAsync(p => p.UserGroupId == userGroup.Id, conn, trans);
                        await _userGroupPermissionRepository.DeleteAsync(p => p.UserGroupId == userGroup.Id, conn,
                                                                         trans);
                        await _userGroupDataPermissionOrgRelationRepository.DeleteAsync(
                            p => p.UserGroupId == userGroup.Id, conn, trans);
                        await _userGroupOrganizationRepository.DeleteAsync(p => p.UserGroupId == userGroup.Id, conn,
                                                                           trans);
                        foreach (var roleId in input.RoleIds)
                        {
                            await _userGroupRoleRepository.InsertAsync(
                                new UserGroupRole {
                                UserGroupId = userGroup.Id, RoleId = roleId
                            }, conn, trans);
                        }

                        if (!input.IsAllOrg)
                        {
                            if (!await UpdateOrgIdsEqExistOrgIds(input.OrgIds, userGroupOrganizationIds, userGroup.Id))
                            {
                                throw new BusinessException("用户组所属类型为自定义的且已经存在用户的,不允许修改用户组的所属部门");
                            }

                            foreach (var orgId in input.OrgIds)
                            {
                                var userGroupOrg = new UserGroupOrganization()
                                {
                                    UserGroupId = userGroup.Id, 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   = userGroup.Id,
                                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   = userGroup.Id,
                                    OrgId         = orgId,
                                    CreationTime  = DateTime.Now,
                                    CreatorUserId = _session.UserId,
                                    TenantId      = _session.TenantId
                                });
                            }
                            await conn.ExecuteAsync(insertDataPermissionOrgSql, dataPermissionOrgDatas, trans);
                        }
                        await RemoveUserGroupCheckPemissionCache(userGroup.Id);
                    }, Connection);
                });
            }
        }