/// <summary> /// /// </summary> /// <param name="relationUserRoleDto"></param> /// <returns></returns> public int AddRoleForUser(RelationUserRoleDto relationUserRoleDto) { IList <TRelationUserRole> relationUserRoles = new List <TRelationUserRole>(); foreach (string role in relationUserRoleDto.RoleIds) { TRelationUserRole relationUserRole = new TRelationUserRole { Id = IdentityHelper.NewSequentialGuid().ToString("N"), UserId = relationUserRoleDto.UserId, RoleId = role, RoleCode = IocUnity.Get <RepositoryRole>().GetCode(role) }; relationUserRoles.Add(relationUserRole); } int count = 0; IocUnity.Get <RepositoryRelationUserRole>().DapperRepository.ExcuteTransaction(tranc => { IocUnity.Get <RepositoryRelationUserRole>().Delete(relationUserRoleDto.UserId); count = IocUnity.Get <RepositoryRelationUserRole>().Insert(relationUserRoles); }); return(count); }
/// <summary> /// /// </summary> /// <param name="relationUserPrivilegeDto"></param> /// <returns></returns> public int AddPrivilegeRoleForUser(UserAddRoleAndPrivilege relationUserPrivilegeDto) { IList <TRelationUserPrivilege> relationUserPrivileges = new List <TRelationUserPrivilege>(); if (relationUserPrivilegeDto.Privileges != null) { foreach (string Privilege in relationUserPrivilegeDto.Privileges) { TRelationUserPrivilege relationUserPrivilege = new TRelationUserPrivilege { Id = IdentityHelper.NewSequentialGuid().ToString("N"), UserId = relationUserPrivilegeDto.UserId, PrivilegeId = Privilege, PrivilegeCode = IocUnity.Get <RepositoryPrivilege>().GetPrivilegeCode(Privilege) }; relationUserPrivileges.Add(relationUserPrivilege); } } if (relationUserPrivilegeDto.PrivilegeCodes != null) { foreach (string privilege in relationUserPrivilegeDto.PrivilegeCodes) { TRelationUserPrivilege relationUserPrivilege = new TRelationUserPrivilege { Id = IdentityHelper.NewSequentialGuid().ToString("N"), UserId = relationUserPrivilegeDto.UserId, PrivilegeId = IocUnity.Get <RepositoryPrivilege>().GetPrivilegeId(privilege), PrivilegeCode = privilege }; relationUserPrivileges.Add(relationUserPrivilege); } } IList <TRelationUserRole> relationUserRoles = new List <TRelationUserRole>(); if (relationUserPrivilegeDto.Roles != null) { foreach (string role in relationUserPrivilegeDto.Roles) { TRelationUserRole relationUserRole = new TRelationUserRole { Id = IdentityHelper.NewSequentialGuid().ToString("N"), UserId = relationUserPrivilegeDto.UserId, RoleId = role, RoleCode = IocUnity.Get <RepositoryRole>().GetCode(role) }; relationUserRoles.Add(relationUserRole); } } if (relationUserPrivilegeDto.RoleCodes != null) { foreach (string role in relationUserPrivilegeDto.RoleCodes) { TRelationUserRole relationUserRole = new TRelationUserRole { Id = IdentityHelper.NewSequentialGuid().ToString("N"), UserId = relationUserPrivilegeDto.UserId, RoleCode = role, RoleId = IocUnity.Get <RepositoryRole>().GetId(role) }; relationUserRoles.Add(relationUserRole); } } int count = 0; IocUnity.Get <RepositoryRelationUserPrivilege>().DapperRepository.ExcuteTransaction(tranc => { IocUnity.Get <RepositoryRelationUserPrivilege>().Delete(relationUserPrivilegeDto.UserId); count = IocUnity.Get <RepositoryRelationUserPrivilege>().Insert(relationUserPrivileges); IocUnity.Get <RepositoryRelationUserRole>().Delete(relationUserPrivilegeDto.UserId); count += IocUnity.Get <RepositoryRelationUserRole>().Insert(relationUserRoles); }); return(count); }