Update user register to distinguish admin and user authorities

This commit is contained in:
Pablo Bonilla 2021-07-06 21:31:12 -06:00
parent 57d492d21f
commit 6395f3d8c8
No known key found for this signature in database
GPG Key ID: 46877262B8DE47E2
6 changed files with 38 additions and 7 deletions

View File

@ -156,8 +156,7 @@ public class UserService {
* Modified to register extra user data
* name, iconoPerfil, fechaNacimiento, estado, pais
*/
public User registerUser(AdminUserDTO userDTO, String password, String name, Integer profileIcon) {
System.out.println(name);
public User registerUser(AdminUserDTO userDTO, String password, String name, Integer profileIcon, Integer isAdmin) {
userRepository
.findOneByLogin(userDTO.getLogin().toLowerCase())
.ifPresent(
@ -195,7 +194,12 @@ public class UserService {
// new user gets registration key
newUser.setActivationKey(RandomUtil.generateActivationKey());
Set<Authority> authorities = new HashSet<>();
// Check whether it's an ADMIN or USER and apply authorities
if (isAdmin == 1) {
authorityRepository.findById(AuthoritiesConstants.ADMIN).ifPresent(authorities::add);
}
authorityRepository.findById(AuthoritiesConstants.USER).ifPresent(authorities::add);
newUser.setAuthorities(authorities);
userRepository.save(newUser);
this.clearUserCaches(newUser);

View File

@ -66,7 +66,8 @@ public class AccountResource {
managedUserVM,
managedUserVM.getPassword(),
managedUserVM.getName(),
managedUserVM.getProfileIcon()
managedUserVM.getProfileIcon(),
managedUserVM.getIsAdmin()
);
mailService.sendActivationEmail(user);
}

View File

@ -22,6 +22,8 @@ public class ManagedUserVM extends AdminUserDTO {
private Integer profileIcon;
private Integer isAdmin;
public ManagedUserVM() {
// Empty constructor needed for Jackson.
}
@ -50,6 +52,14 @@ public class ManagedUserVM extends AdminUserDTO {
this.profileIcon = profileIcon;
}
public Integer getIsAdmin() {
return isAdmin;
}
public void setIsAdmin(Integer isAdmin) {
this.isAdmin = isAdmin;
}
// prettier-ignore
@Override
public String toString() {

View File

@ -64,6 +64,7 @@ describe('Component Tests', () => {
langKey: 'es',
name: '',
profileIcon: 1,
isAdmin: 0,
});
expect(comp.success).toBe(true);
expect(comp.errorUserExists).toBe(false);

View File

@ -1,7 +1,8 @@
import { Component, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { Component, OnInit, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
import { FormBuilder, Validators } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import { Router } from '@angular/router';
import { EMAIL_ALREADY_USED_TYPE, LOGIN_ALREADY_USED_TYPE } from 'app/config/error.constants';
import { RegisterService } from './register.service';
@ -11,7 +12,7 @@ import { RegisterService } from './register.service';
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss'],
})
export class RegisterComponent implements AfterViewInit {
export class RegisterComponent implements OnInit, AfterViewInit {
// @ViewChild('name', { static: false })
// name?: ElementRef;
@ -52,6 +53,7 @@ export class RegisterComponent implements AfterViewInit {
errorEmailExists = false;
errorUserExists = false;
success = false;
isAdmin: number = 0;
// Login will be used to store the email as well.
// login: ['', [Validators.required, Validators.minLength(5), Validators.maxLength(254), Validators.email]]
@ -64,6 +66,10 @@ export class RegisterComponent implements AfterViewInit {
constructor(private translateService: TranslateService, private registerService: RegisterService, private fb: FormBuilder) {}
ngOnInit(): void {
this.isAdmin = window.location.href.includes('/account/register') ? 0 : 1;
}
ngAfterViewInit(): void {
// if (this.name) {
// this.name.nativeElement.focus();
@ -86,7 +92,15 @@ export class RegisterComponent implements AfterViewInit {
console.log(name);
this.registerService
.save({ login, email, password, langKey: this.translateService.currentLang, name, profileIcon: this.profileIcon })
.save({
login,
email,
password,
langKey: this.translateService.currentLang,
name,
profileIcon: this.profileIcon,
isAdmin: this.isAdmin,
})
.subscribe(
() => (this.success = true),
response => this.processError(response)

View File

@ -5,6 +5,7 @@ export class Registration {
public password: string,
public langKey: string,
public name: string,
public profileIcon: number
public profileIcon: number,
public isAdmin: number
) {}
}