Skip to content

Commit 45cc84b

Browse files
committed
resolved #96: changed userRole mappings
1 parent 18225cc commit 45cc84b

File tree

15 files changed

+1212
-82
lines changed

15 files changed

+1212
-82
lines changed

angular/src/app/users/create-user/create-user.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class CreateUserComponent extends AppComponentBase implements OnInit {
5454
}
5555
});
5656

57-
this.user.roles = roles;
57+
this.user.roleNames = roles;
5858
this.saving = true;
5959
this._userService.create(this.user)
6060
.finally(() => { this.saving = false; })

angular/src/app/users/edit-user/edit-user.component.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { AppComponentBase } from '@shared/app-component-base';
66
import * as _ from "lodash";
77

88
@Component({
9-
selector: 'edit-user-modal',
10-
templateUrl: './edit-user.component.html'
9+
selector: 'edit-user-modal',
10+
templateUrl: './edit-user.component.html'
1111
})
1212
export class EditUserComponent extends AppComponentBase {
1313

@@ -29,29 +29,29 @@ export class EditUserComponent extends AppComponentBase {
2929
super(injector);
3030
}
3131

32-
userInRole(role:RoleDto, user:UserDto): string {
33-
if(user.roles.indexOf(role.displayName)!= -1) {
34-
return "checked";
35-
}
36-
else {
37-
return "";
38-
}
39-
}
32+
userInRole(role: RoleDto, user: UserDto): string {
33+
if (user.roleNames.indexOf(role.normalizedName) !== -1) {
34+
return "checked";
35+
}
36+
else {
37+
return "";
38+
}
39+
}
4040

41-
show(id:number): void {
41+
show(id: number): void {
4242
this._userService.getRoles()
4343
.subscribe((result) => {
4444
this.roles = result.items;
4545
});
4646

47-
this._userService.get(id)
48-
.subscribe(
49-
(result) => {
50-
this.user = result;
51-
this.active = true;
52-
this.modal.show();
53-
}
54-
);
47+
this._userService.get(id)
48+
.subscribe(
49+
(result) => {
50+
this.user = result;
51+
this.active = true;
52+
this.modal.show();
53+
}
54+
);
5555
}
5656

5757
onShown(): void {
@@ -60,13 +60,14 @@ export class EditUserComponent extends AppComponentBase {
6060

6161
save(): void {
6262
var roles = [];
63-
$(this.modalContent.nativeElement).find("[name=role]").each(function(ind:number, elem:Element){
64-
if($(elem).is(":checked")){
63+
$(this.modalContent.nativeElement).find("[name=role]").each(function (ind: number, elem: Element) {
64+
if ($(elem).is(":checked")) {
6565
roles.push(elem.getAttribute("value").valueOf());
6666
}
6767
});
6868

69-
this.user.roles = roles;
69+
this.user.roleNames = roles;
70+
7071
this.saving = true;
7172
this._userService.update(this.user)
7273
.finally(() => { this.saving = false; })

angular/src/shared/service-proxies/service-proxies.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,7 @@ export class CreateUserDto {
20582058
surname: string;
20592059
emailAddress: string;
20602060
isActive: boolean;
2061-
roles: string[];
2061+
roleNames: string[];
20622062
password: string;
20632063
constructor(data?: any) {
20642064
if (data !== undefined) {
@@ -2067,10 +2067,10 @@ export class CreateUserDto {
20672067
this.surname = data["surname"] !== undefined ? data["surname"] : null;
20682068
this.emailAddress = data["emailAddress"] !== undefined ? data["emailAddress"] : null;
20692069
this.isActive = data["isActive"] !== undefined ? data["isActive"] : null;
2070-
if (data["roles"] && data["roles"].constructor === Array) {
2071-
this.roles = [];
2072-
for (let item of data["roles"])
2073-
this.roles.push(item);
2070+
if (data["roleNames"] && data["roleNames"].constructor === Array) {
2071+
this.roleNames = [];
2072+
for (let item of data["roleNames"])
2073+
this.roleNames.push(item);
20742074
}
20752075
this.password = data["password"] !== undefined ? data["password"] : null;
20762076
}
@@ -2087,10 +2087,10 @@ export class CreateUserDto {
20872087
data["surname"] = this.surname !== undefined ? this.surname : null;
20882088
data["emailAddress"] = this.emailAddress !== undefined ? this.emailAddress : null;
20892089
data["isActive"] = this.isActive !== undefined ? this.isActive : null;
2090-
if (this.roles && this.roles.constructor === Array) {
2091-
data["roles"] = [];
2092-
for (let item of this.roles)
2093-
data["roles"].push(item);
2090+
if (this.roleNames && this.roleNames.constructor === Array) {
2091+
data["roleNames"] = [];
2092+
for (let item of this.roleNames)
2093+
data["roleNames"].push(item);
20942094
}
20952095
data["password"] = this.password !== undefined ? this.password : null;
20962096
return data;
@@ -2115,7 +2115,7 @@ export class UserDto {
21152115
fullName: string;
21162116
lastLoginTime: moment.Moment;
21172117
creationTime: moment.Moment;
2118-
roles: string[];
2118+
roleNames: string[];
21192119
id: number;
21202120
constructor(data?: any) {
21212121
if (data !== undefined) {
@@ -2127,10 +2127,10 @@ export class UserDto {
21272127
this.fullName = data["fullName"] !== undefined ? data["fullName"] : null;
21282128
this.lastLoginTime = data["lastLoginTime"] ? moment(data["lastLoginTime"].toString()) : null;
21292129
this.creationTime = data["creationTime"] ? moment(data["creationTime"].toString()) : null;
2130-
if (data["roles"] && data["roles"].constructor === Array) {
2131-
this.roles = [];
2132-
for (let item of data["roles"])
2133-
this.roles.push(item);
2130+
if (data["roleNames"] && data["roleNames"].constructor === Array) {
2131+
this.roleNames = [];
2132+
for (let item of data["roleNames"])
2133+
this.roleNames.push(item);
21342134
}
21352135
this.id = data["id"] !== undefined ? data["id"] : null;
21362136
}
@@ -2150,10 +2150,10 @@ export class UserDto {
21502150
data["fullName"] = this.fullName !== undefined ? this.fullName : null;
21512151
data["lastLoginTime"] = this.lastLoginTime ? this.lastLoginTime.toISOString() : null;
21522152
data["creationTime"] = this.creationTime ? this.creationTime.toISOString() : null;
2153-
if (this.roles && this.roles.constructor === Array) {
2154-
data["roles"] = [];
2155-
for (let item of this.roles)
2156-
data["roles"].push(item);
2153+
if (this.roleNames && this.roleNames.constructor === Array) {
2154+
data["roleNames"] = [];
2155+
for (let item of this.roleNames)
2156+
data["roleNames"].push(item);
21572157
}
21582158
data["id"] = this.id !== undefined ? this.id : null;
21592159
return data;
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Abp.Authorization;
1+
using Abp.Authorization;
72
using Abp.Authorization.Roles;
83
using Abp.Authorization.Users;
94
using Abp.Dependency;
@@ -22,17 +17,7 @@ public RoleMapProfile()
2217
CreateMap<RolePermissionSetting, string>().ConvertUsing(r => r.Name);
2318

2419
CreateMap<CreateRoleDto, Role>().ForMember(x => x.Permissions, opt => opt.Ignore());
25-
CreateMap<RoleDto, Role>().ForMember(x => x.Permissions, opt => opt.Ignore());
26-
27-
IRepository<Role, int> repository = IocManager.Instance.Resolve<IRepository<Role, int>>();
28-
// User and role
29-
CreateMap<UserRole, string>().ConvertUsing((r) => {
30-
//TODO: Fix, this seems hacky
31-
Role role = repository.FirstOrDefault(r.RoleId);
32-
return role.DisplayName;
33-
});
34-
35-
IocManager.Instance.Release(repository);
20+
CreateMap<RoleDto, Role>().ForMember(x => x.Permissions, opt => opt.Ignore());
3621
}
3722
}
3823
}

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/Users/Dto/CreateUserDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class CreateUserDto
2828

2929
public bool IsActive { get; set; }
3030

31-
public string[] Roles { get; set; }
31+
public string[] RoleNames { get; set; }
3232

3333
[Required]
3434
[StringLength(AbpUserBase.MaxPlainPasswordLength)]

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/Users/Dto/UserDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public class UserDto : EntityDto<long>
3535

3636
public DateTime CreationTime { get; set; }
3737

38-
public string[] Roles { get; set; }
38+
public string[] RoleNames { get; set; }
3939
}
4040
}

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/Users/Dto/UserMapProfile.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using AbpCompanyName.AbpProjectName.Authorization.Users;
1+
using AbpCompanyName.AbpProjectName.Authorization.Users;
72
using AutoMapper;
83

94
namespace AbpCompanyName.AbpProjectName.Users.Dto
@@ -12,7 +7,6 @@ public class UserMapProfile : Profile
127
{
138
public UserMapProfile()
149
{
15-
1610
CreateMap<UserDto, User>();
1711
CreateMap<UserDto, User>().ForMember(x => x.Roles, opt => opt.Ignore());
1812

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/Users/UserAppService.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,24 @@ namespace AbpCompanyName.AbpProjectName.Users
2020
public class UserAppService : AsyncCrudAppService<User, UserDto, long, PagedResultRequestDto, CreateUserDto, UserDto>, IUserAppService
2121
{
2222
private readonly UserManager _userManager;
23+
private readonly RoleManager _roleManager;
2324
private readonly IPasswordHasher<User> _passwordHasher;
2425
private readonly IRepository<Role> _roleRepository;
2526

26-
public UserAppService(IRepository<User, long> repository, UserManager userManager, IPasswordHasher<User> passwordHasher, IRepository<Role> roleRepository)
27+
public UserAppService(
28+
IRepository<User, long> repository,
29+
UserManager userManager,
30+
IPasswordHasher<User> passwordHasher,
31+
IRepository<Role> roleRepository,
32+
RoleManager roleManager)
2733
: base(repository)
2834
{
2935
_userManager = userManager;
3036
_passwordHasher = passwordHasher;
3137
_roleRepository = roleRepository;
38+
_roleManager = roleManager;
3239
}
33-
40+
3441
public override async Task<UserDto> Create(CreateUserDto input)
3542
{
3643
CheckCreatePermission();
@@ -43,9 +50,9 @@ public override async Task<UserDto> Create(CreateUserDto input)
4350

4451
CheckErrors(await _userManager.CreateAsync(user));
4552

46-
if (input.Roles != null)
53+
if (input.RoleNames != null)
4754
{
48-
CheckErrors(await _userManager.SetRoles(user, input.Roles));
55+
CheckErrors(await _userManager.SetRoles(user, input.RoleNames));
4956
}
5057

5158
CurrentUnitOfWork.SaveChanges();
@@ -63,9 +70,9 @@ public override async Task<UserDto> Update(UserDto input)
6370

6471
CheckErrors(await _userManager.UpdateAsync(user));
6572

66-
if (input.Roles != null)
73+
if (input.RoleNames != null)
6774
{
68-
CheckErrors(await _userManager.SetRoles(user, input.Roles));
75+
CheckErrors(await _userManager.SetRoles(user, input.RoleNames));
6976
}
7077

7178
return await Get(input);
@@ -75,7 +82,7 @@ public override async Task Delete(EntityDto<long> input)
7582
{
7683
var user = await _userManager.GetUserByIdAsync(input.Id);
7784
await _userManager.DeleteAsync(user);
78-
}
85+
}
7986

8087
public async Task<ListResultDto<RoleDto>> GetRoles()
8188
{
@@ -95,7 +102,15 @@ protected override void MapToEntity(UserDto input, User user)
95102
ObjectMapper.Map(input, user);
96103
user.SetNormalizedNames();
97104
}
98-
105+
106+
protected override UserDto MapToEntityDto(User user)
107+
{
108+
var roles = _roleManager.Roles.Where(r => user.Roles.Any(ur => ur.RoleId == r.Id)).Select(r => r.NormalizedName);
109+
var userDto = base.MapToEntityDto(user);
110+
userDto.RoleNames = roles.ToArray();
111+
return userDto;
112+
}
113+
99114
protected override IQueryable<User> CreateFilteredQuery(PagedResultRequestDto input)
100115
{
101116
return Repository.GetAllIncluding(x => x.Roles);
@@ -105,7 +120,7 @@ protected override async Task<User> GetEntityByIdAsync(long id)
105120
{
106121
return await Repository.GetAllIncluding(x => x.Roles).FirstOrDefaultAsync(x => x.Id == id);
107122
}
108-
123+
109124
protected override IQueryable<User> ApplySorting(IQueryable<User> query, PagedResultRequestDto input)
110125
{
111126
return query.OrderBy(r => r.UserName);

0 commit comments

Comments
 (0)