diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 5ad03e3..0795602 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -52,6 +52,7 @@ const routes: Routes = [ {path : 'list-all-people', component: LenderUploadsComponent, canActivate: [AuthGuard] }, {path : 'people-add', component: PeopleAddComponent, canActivate: [AuthGuard] }, {path : 'profile', component: ProfileComponent, canActivate: [AuthGuard] }, + {path : 'profile/:id', component: ProfileComponent, canActivate: [AuthGuard] }, {path : 'e403', component: E403Component, }, ]; diff --git a/src/app/models/user-extra.model.ts b/src/app/models/user-extra.model.ts index 093a30d..b1183cd 100644 --- a/src/app/models/user-extra.model.ts +++ b/src/app/models/user-extra.model.ts @@ -6,6 +6,7 @@ export class UserExtraModel { public Organization: string; public Enabled: boolean; public Login: string; + public Role?: string; constructor(payload: Partial) { Object.assign(this, payload); @@ -19,6 +20,7 @@ export class UserExtraModel { rt.Organization = ''; rt.Enabled = false; rt.Login = ''; + rt.Role = ''; return rt; } } diff --git a/src/app/profile/broker-profile/broker-profile.component.ts b/src/app/profile/broker-profile/broker-profile.component.ts index c170e65..6559693 100644 --- a/src/app/profile/broker-profile/broker-profile.component.ts +++ b/src/app/profile/broker-profile/broker-profile.component.ts @@ -15,14 +15,20 @@ import {MessageBoxComponent} from '../../message-box/message-box.component'; }) export class BrokerProfileComponent implements OnInit { @Input() public PeopleId = ''; - @Input() public showAdmin: false; @Input() public broker: BrokerModel = BrokerModel.EmptyNew(); @ViewChild('messageBox', {static: true})msgBox: MessageBoxComponent; + public showAdmin = false; constructor( private auth: AuthService, private ps: PeopleService) { } ngOnInit(): void { - this.broker = BrokerModel.getFromUserAndExtra(this.auth.loggedIn.User, this.auth.loggedIn.UserExtra); + this.showAdmin = this.auth.isAdmin(); + + if (this.auth.isAdmin() ) { + // edit someone else + }else { + this.broker = BrokerModel.getFromUserAndExtra(this.auth.loggedIn.User, this.auth.loggedIn.UserExtra); + } } public save(brokerForm: NgForm): void{ diff --git a/src/app/profile/change-password/change-password.component.html b/src/app/profile/change-password/change-password.component.html index ab5e253..7800532 100644 --- a/src/app/profile/change-password/change-password.component.html +++ b/src/app/profile/change-password/change-password.component.html @@ -5,7 +5,7 @@
- + diff --git a/src/app/profile/change-password/change-password.component.ts b/src/app/profile/change-password/change-password.component.ts index 026bb42..22d2f68 100644 --- a/src/app/profile/change-password/change-password.component.ts +++ b/src/app/profile/change-password/change-password.component.ts @@ -14,6 +14,7 @@ export class ChangePasswordComponent implements OnInit { @ViewChild('messageBox', {static: true}) msgBox: MessageBoxComponent; public changePassword = false; + public isAdmin = false; public ChangePassForm: FormGroup = new FormGroup({ OldPassword: new FormControl(), @@ -23,6 +24,9 @@ export class ChangePasswordComponent implements OnInit { constructor(private auth: AuthService, private ps: PeopleService) { } ngOnInit(): void { + if (this.auth.isAdmin() && this.PeopleId !== '' && !this.auth.isCurrentUser(this.PeopleId)) { + this.isAdmin = this.auth.isAdmin(); + } } public hidePass(): void{ if ( this.changePassword ) { @@ -31,7 +35,7 @@ export class ChangePasswordComponent implements OnInit { } public savePassword(): void{ - this.ps.savePassword('0', this.ChangePassForm.value).subscribe( + this.ps.savePassword(this.PeopleId, this.ChangePassForm.value).subscribe( () => { this.changePassword = false; this.msgBox.Show('Password Changed'); @@ -52,6 +56,10 @@ export class ChangePasswordComponent implements OnInit { } public canChangePassword(): boolean { - return this.auth.isBroker() || this.auth.isAdmin(); + if ( this.auth.isCurrentUser(this.PeopleId) ) { + return this.auth.isBroker() || this.auth.isAdmin(); + }else{ + return this.auth.isAdmin() ; + } } } diff --git a/src/app/profile/people-profile/people-profile.component.ts b/src/app/profile/people-profile/people-profile.component.ts index 543ce27..e1b1e0b 100644 --- a/src/app/profile/people-profile/people-profile.component.ts +++ b/src/app/profile/people-profile/people-profile.component.ts @@ -13,8 +13,8 @@ import {PeopleModel} from '../../models/people.model'; }) export class PeopleProfileComponent implements OnInit { @Input() PeopleId = ''; - public People: PeopleModel = PeopleModel.EmptyNew(); - public avatarUrl = 'url(https://svr2021.lawipac.com:8080/api/v1/avatar/1000)' ; + @Input() public People: PeopleModel = PeopleModel.EmptyNew(); + public avatarUrl = 'url(https://via.placeholder.com/128)' ; public myRestrictions: FileRestrictions = { allowedExtensions: ['.jpg', '.png', '.jpeg'], maxFileSize: 2194304 @@ -25,8 +25,26 @@ export class PeopleProfileComponent implements OnInit { constructor(private auth: AuthService, private ps: PeopleService) { } ngOnInit(): void { - this.People = this.auth.loggedIn.User; - this.avatarUrl = 'url(' + this.auth.getUrl('avatar/' + this.PeopleId) + ')'; + if (this.auth.isAdmin() && !this.auth.isCurrentUser(this.PeopleId)) { + // edit someone else + if ( this.People.Id === '' && this.PeopleId !== '') {// we need to load user by ourself + this.ps.getPeopleById(this.PeopleId).subscribe( + ppl => { + this.People = new PeopleModel( + ppl.Id, + ppl.First, + ppl.Last, + ppl.Middle, + ppl.Title, + ppl.Display, + ppl.Nick + ); + } + ); + } + }else{ + this.People = this.auth.loggedIn.User; + } this.avatarUrl = 'url(' + this.auth.getUrl('avatar/' + this.PeopleId) + ')'; } diff --git a/src/app/profile/profile.component.html b/src/app/profile/profile.component.html index 561969d..6001020 100644 --- a/src/app/profile/profile.component.html +++ b/src/app/profile/profile.component.html @@ -4,11 +4,11 @@
- +

- +

- +


diff --git a/src/app/profile/profile.component.ts b/src/app/profile/profile.component.ts index 148d38d..b896dac 100644 --- a/src/app/profile/profile.component.ts +++ b/src/app/profile/profile.component.ts @@ -5,6 +5,10 @@ import {PeopleService} from '../service/people.service'; import {PeopleProfileComponent} from './people-profile/people-profile.component'; import {BrokerProfileComponent} from './broker-profile/broker-profile.component'; import {UserProfileComponent} from './user-profile/user-profile.component'; +import {ActivatedRoute} from '@angular/router'; +import {BrokerModel} from '../models/broker.model'; +import {PeopleModel} from '../models/people.model'; +import {UserExtraModel} from '../models/user-extra.model'; @Component({ selector: 'app-profile', @@ -17,19 +21,66 @@ export class ProfileComponent implements OnInit { @ViewChild('userProfile', {static: true}) up: UserProfileComponent; @ViewChild('brokerProfile', {static: true}) bp: BrokerProfileComponent; - public role = 'client'; + public role = 'unknown'; + public UserExtra: UserExtraModel = UserExtraModel.EmptyNew(); + public People: PeopleModel = PeopleModel.EmptyNew(); + public broker: BrokerModel = BrokerModel.EmptyNew(); - constructor(private auth: AuthService, private ps: PeopleService) { } + public isValidPeople = true; + public isValidBroker = true; + + constructor(private auth: AuthService, private ps: PeopleService, private actRoute: ActivatedRoute) { } ngOnInit(): void { - this.role = this.auth.loggedIn.role; - this.PeopleId = this.auth.loggedIn.User.Id; - console.log('profile people id', this.PeopleId); + const id = this.actRoute.snapshot.params.id; + if ( id !== undefined && id.length > 10 ) { + this.PeopleId = id; + } + + if (this.auth.isAdmin() && this.PeopleId !== '' && !this.auth.isCurrentUser(this.PeopleId)) { // admin editing someone else + this.loadOtherPeople(); + this.loadOtherPeopleExtra(); + + }else { // edit himself + this.role = this.auth.loggedIn.role; + this.PeopleId = this.auth.loggedIn.User.Id; + } } + private loadOtherPeople(): void{ + this.isValidPeople = false; + this.ps.getPeopleById(this.PeopleId).subscribe( + ppl => { + this.isValidPeople = ppl.Id === this.PeopleId; + this.People = new PeopleModel( + ppl.Id, + ppl.First, + ppl.Last, + ppl.Middle, + ppl.Title, + ppl.Display, + ppl.Nick + ); + } + ); + } + + private loadOtherPeopleExtra(): void { + this.isValidBroker = false; + this.ps.getPeopleExtraById(this.PeopleId).subscribe( + resp => { + this.isValidBroker = resp.License !== ''; + this.UserExtra = new UserExtraModel(resp); + this.role = this.UserExtra.Role; + if (this.role === 'broker') { + this.broker = BrokerModel.getFromUserAndExtra(this.People, this.UserExtra); + } + } + ); + } public showPeople(): boolean { - return true; // always true + return this.isValidPeople; // always true } public showUser(): boolean { @@ -49,15 +100,26 @@ export class ProfileComponent implements OnInit { } public isAdmin(): boolean { - return this.auth.isAdmin(); + if ( this.auth.isCurrentUser(this.PeopleId) ){ + return this.auth.isAdmin(); + }else{ + return this.role === 'admin'; + } } public isBroker(): boolean { - return this.auth.isBroker(); + if ( this.auth.isCurrentUser(this.PeopleId) ) { + return this.auth.isBroker(); + }else{ + return this.role === 'broker'; + } } public isUser(): boolean { - return this.auth.isUser(); + if ( this.auth.isCurrentUser(this.PeopleId) ) { + return this.auth.isUser(); + }else{ + return this.role === 'user'; + } } - } diff --git a/src/app/profile/user-profile/user-profile.component.html b/src/app/profile/user-profile/user-profile.component.html index 25fc365..d8e707b 100644 --- a/src/app/profile/user-profile/user-profile.component.html +++ b/src/app/profile/user-profile/user-profile.component.html @@ -15,7 +15,7 @@
diff --git a/src/app/profile/user-profile/user-profile.component.ts b/src/app/profile/user-profile/user-profile.component.ts index ffd0c98..6795bbe 100644 --- a/src/app/profile/user-profile/user-profile.component.ts +++ b/src/app/profile/user-profile/user-profile.component.ts @@ -4,6 +4,8 @@ import {NgForm} from '@angular/forms'; import {AuthService} from '../../service/auth.service'; import {HttpClient} from '@angular/common/http'; import {MessageBoxComponent} from '../../message-box/message-box.component'; +import {BrokerModel} from '../../models/broker.model'; +import {PeopleService} from '../../service/people.service'; @Component({ selector: 'app-user-profile', @@ -12,16 +14,29 @@ import {MessageBoxComponent} from '../../message-box/message-box.component'; }) export class UserProfileComponent implements OnInit { @Input() public PeopleId = ''; - @Input() public showAdmin: false; - - public UserExtra: UserExtraModel = UserExtraModel.EmptyNew(); + @Input() public UserExtra: UserExtraModel = UserExtraModel.EmptyNew(); @ViewChild('messageBox', {static: true}) msgBox: MessageBoxComponent; - constructor(private auth: AuthService, private http: HttpClient) { } + public showAdmin = false; + public isCurrentUser = false; + constructor(private auth: AuthService, private http: HttpClient, private ps: PeopleService) { } ngOnInit(): void { - this.PeopleId = this.auth.loggedIn.User.Id; - this.UserExtra = this.auth.loggedIn.UserExtra; + this.showAdmin = this.auth.isAdmin(); + this.isCurrentUser = this.auth.isCurrentUser(this.PeopleId); + if (this.auth.isAdmin() && !this.auth.isCurrentUser(this.PeopleId)) { + // edit other people, + if ( this.PeopleId !== '' && this.UserExtra.Login === '' ){ // we need to load by ourself + this.ps.getPeopleExtraById(this.PeopleId).subscribe( + resp => { + this.UserExtra = new UserExtraModel(resp); + } + ); + } + }else{ + this.PeopleId = this.auth.loggedIn.User.Id; + this.UserExtra = this.auth.loggedIn.UserExtra; + } } public save(userForm: NgForm): void { diff --git a/src/app/service/auth.service.ts b/src/app/service/auth.service.ts index d9e3ae4..0d7afa7 100644 --- a/src/app/service/auth.service.ts +++ b/src/app/service/auth.service.ts @@ -149,4 +149,8 @@ export class AuthService { public isUser(): boolean { return this.loggedIn.login === true; } + + public isCurrentUser(id: string): boolean { + return this.loggedIn.login === true && this.loggedIn.User !== undefined && this.loggedIn.User.Id === id; + } } diff --git a/src/app/service/people.service.ts b/src/app/service/people.service.ts index 5ab2372..e365084 100644 --- a/src/app/service/people.service.ts +++ b/src/app/service/people.service.ts @@ -6,6 +6,7 @@ import {PeopleModel} from '../models/people.model'; import {BrokerModel} from '../models/broker.model'; import {LoanModel} from '../models/loan.model'; import {ChangePassword} from '../models/change-password.model'; +import {UserExtraModel} from '../models/user-extra.model'; @Injectable({providedIn: 'root'}) export class PeopleService { @@ -14,6 +15,9 @@ export class PeopleService { public getPeopleById(id: string): Observable { return this.http.get(this.auth.getUrl('people/' + id)); } + public getPeopleExtraById(id: string): Observable { + return this.http.get(this.auth.getUrl('people-extra/' + id)); + } public getPeopleList(filter: string): Observable<{Count: number, List: PeopleModel[]}> { const params = new HttpParams().set('filter', filter);