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 * Modified to register extra user data
* name, iconoPerfil, fechaNacimiento, estado, pais * name, iconoPerfil, fechaNacimiento, estado, pais
*/ */
public User registerUser(AdminUserDTO userDTO, String password, String name, Integer profileIcon) { public User registerUser(AdminUserDTO userDTO, String password, String name, Integer profileIcon, Integer isAdmin) {
System.out.println(name);
userRepository userRepository
.findOneByLogin(userDTO.getLogin().toLowerCase()) .findOneByLogin(userDTO.getLogin().toLowerCase())
.ifPresent( .ifPresent(
@ -195,7 +194,12 @@ public class UserService {
// new user gets registration key // new user gets registration key
newUser.setActivationKey(RandomUtil.generateActivationKey()); newUser.setActivationKey(RandomUtil.generateActivationKey());
Set<Authority> authorities = new HashSet<>(); 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); authorityRepository.findById(AuthoritiesConstants.USER).ifPresent(authorities::add);
newUser.setAuthorities(authorities); newUser.setAuthorities(authorities);
userRepository.save(newUser); userRepository.save(newUser);
this.clearUserCaches(newUser); this.clearUserCaches(newUser);

View File

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

View File

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

View File

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

View File

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