From 6395f3d8c820ea581016a7db24ef3141f47ef4f7 Mon Sep 17 00:00:00 2001 From: Pablo Bonilla Date: Tue, 6 Jul 2021 21:31:12 -0600 Subject: [PATCH 1/3] Update user register to distinguish admin and user authorities --- .../org/datasurvey/service/UserService.java | 8 ++++++-- .../datasurvey/web/rest/AccountResource.java | 3 ++- .../datasurvey/web/rest/vm/ManagedUserVM.java | 10 ++++++++++ .../register/register.component.spec.ts | 1 + .../account/register/register.component.ts | 20 ++++++++++++++++--- .../app/account/register/register.model.ts | 3 ++- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/datasurvey/service/UserService.java b/src/main/java/org/datasurvey/service/UserService.java index 797de9b..e1d8bb5 100644 --- a/src/main/java/org/datasurvey/service/UserService.java +++ b/src/main/java/org/datasurvey/service/UserService.java @@ -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 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); diff --git a/src/main/java/org/datasurvey/web/rest/AccountResource.java b/src/main/java/org/datasurvey/web/rest/AccountResource.java index 7d594ca..493ece3 100644 --- a/src/main/java/org/datasurvey/web/rest/AccountResource.java +++ b/src/main/java/org/datasurvey/web/rest/AccountResource.java @@ -66,7 +66,8 @@ public class AccountResource { managedUserVM, managedUserVM.getPassword(), managedUserVM.getName(), - managedUserVM.getProfileIcon() + managedUserVM.getProfileIcon(), + managedUserVM.getIsAdmin() ); mailService.sendActivationEmail(user); } diff --git a/src/main/java/org/datasurvey/web/rest/vm/ManagedUserVM.java b/src/main/java/org/datasurvey/web/rest/vm/ManagedUserVM.java index 9717f34..3d1dc73 100644 --- a/src/main/java/org/datasurvey/web/rest/vm/ManagedUserVM.java +++ b/src/main/java/org/datasurvey/web/rest/vm/ManagedUserVM.java @@ -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() { diff --git a/src/main/webapp/app/account/register/register.component.spec.ts b/src/main/webapp/app/account/register/register.component.spec.ts index 30c967c..422a382 100644 --- a/src/main/webapp/app/account/register/register.component.spec.ts +++ b/src/main/webapp/app/account/register/register.component.spec.ts @@ -64,6 +64,7 @@ describe('Component Tests', () => { langKey: 'es', name: '', profileIcon: 1, + isAdmin: 0, }); expect(comp.success).toBe(true); expect(comp.errorUserExists).toBe(false); diff --git a/src/main/webapp/app/account/register/register.component.ts b/src/main/webapp/app/account/register/register.component.ts index 59f1804..8d558b6 100644 --- a/src/main/webapp/app/account/register/register.component.ts +++ b/src/main/webapp/app/account/register/register.component.ts @@ -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) diff --git a/src/main/webapp/app/account/register/register.model.ts b/src/main/webapp/app/account/register/register.model.ts index eca3751..3a3a97d 100644 --- a/src/main/webapp/app/account/register/register.model.ts +++ b/src/main/webapp/app/account/register/register.model.ts @@ -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 ) {} } From 3f723189b49c527aab34e913d73e284223be489a Mon Sep 17 00:00:00 2001 From: Pablo Bonilla Date: Tue, 6 Jul 2021 22:59:31 -0600 Subject: [PATCH 2/3] Add registrar usuario administrador por medio de la plataforma --- .../account/register/register.component.ts | 9 +- .../usuario-extra-update.component.html | 340 ++++++++++++------ .../usuario-extra-update.component.spec.ts | 302 ++++++---------- .../update/usuario-extra-update.component.ts | 232 +++++------- .../usuario-extra/usuario-extra.module.ts | 3 +- src/main/webapp/i18n/es/register.json | 1 + 6 files changed, 452 insertions(+), 435 deletions(-) diff --git a/src/main/webapp/app/account/register/register.component.ts b/src/main/webapp/app/account/register/register.component.ts index 8d558b6..24a7542 100644 --- a/src/main/webapp/app/account/register/register.component.ts +++ b/src/main/webapp/app/account/register/register.component.ts @@ -12,7 +12,7 @@ import { RegisterService } from './register.service'; templateUrl: './register.component.html', styleUrls: ['./register.component.scss'], }) -export class RegisterComponent implements OnInit, AfterViewInit { +export class RegisterComponent implements AfterViewInit { // @ViewChild('name', { static: false }) // name?: ElementRef; @@ -53,7 +53,6 @@ export class RegisterComponent implements OnInit, 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]] @@ -66,10 +65,6 @@ export class RegisterComponent implements OnInit, 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(); @@ -99,7 +94,7 @@ export class RegisterComponent implements OnInit, AfterViewInit { langKey: this.translateService.currentLang, name, profileIcon: this.profileIcon, - isAdmin: this.isAdmin, + isAdmin: 0, }) .subscribe( () => (this.success = true), diff --git a/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html b/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html index 68ee81c..9c332db 100644 --- a/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html +++ b/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html @@ -1,118 +1,246 @@ -
-
-
-

- Create or edit a Usuario Extra -

+
+
+
+
+ -
- - -
- - -
- -
- - -
- - This field is required. - +
+
+

+ REGISTRAR ADMIN +

+

Ingrese los datos para registrar a un admin.

-
-
- - -
- -
- -
- +
+ Registration saved! Please check your email for confirmation.
-
-
- - -
- - This field is required. - +
+ Registration failed! Please try again later.
-
-
- - -
+
+ Email is already in use! Please choose another one. +
-
- - +
+ The password and its confirmation do not match! +
+ + +
+
+ + + +
+ + Your name is required. + + + + Your name is invalid. + + + + Your name is required to be at least 2 characters. + + + + Your name cannot be longer than 50 characters. + +
+
+
+ +
+
+ + + +
+ + Your email is required. + + + + Your email is invalid. + + + + Your email is required to be at least 5 characters. + + + + Your email cannot be longer than 100 characters. + +
+
+
+ +
+
+ + + +
+ + Your password is required. + + + + Your password is required to be at least 4 characters. + + + + Your password cannot be longer than 50 characters. + +
+
+
+ +
+
+ + + +
+ + Your confirmation password is required. + + + + Your confirmation password is required to be at least 4 characters. + + + + Your confirmation password cannot be longer than 50 characters. + +
+
+
+ +
+
+ + +
+
+ +
+ +
+
- -
- - - -
- +
diff --git a/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.spec.ts b/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.spec.ts index d980272..c2e698a 100644 --- a/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.spec.ts +++ b/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.spec.ts @@ -1,212 +1,144 @@ -jest.mock('@angular/router'); +jest.mock('@ngx-translate/core'); -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { HttpResponse } from '@angular/common/http'; +import { ComponentFixture, TestBed, waitForAsync, inject, tick, fakeAsync } from '@angular/core/testing'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { FormBuilder } from '@angular/forms'; -import { ActivatedRoute } from '@angular/router'; -import { of, Subject } from 'rxjs'; +import { of, throwError } from 'rxjs'; +import { TranslateService } from '@ngx-translate/core'; -import { UsuarioExtraService } from '../service/usuario-extra.service'; -import { IUsuarioExtra, UsuarioExtra } from '../usuario-extra.model'; - -import { IUser } from 'app/entities/user/user.model'; -import { UserService } from 'app/entities/user/user.service'; -import { IPlantilla } from 'app/entities/plantilla/plantilla.model'; -import { PlantillaService } from 'app/entities/plantilla/service/plantilla.service'; +import { EMAIL_ALREADY_USED_TYPE, LOGIN_ALREADY_USED_TYPE } from 'app/config/error.constants'; +import { RegisterService } from 'app/account/register/register.service'; import { UsuarioExtraUpdateComponent } from './usuario-extra-update.component'; describe('Component Tests', () => { - describe('UsuarioExtra Management Update Component', () => { - let comp: UsuarioExtraUpdateComponent; + describe('RegisterComponent', () => { let fixture: ComponentFixture; - let activatedRoute: ActivatedRoute; - let usuarioExtraService: UsuarioExtraService; - let userService: UserService; - let plantillaService: PlantillaService; + let comp: UsuarioExtraUpdateComponent; + + beforeEach( + waitForAsync(() => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + declarations: [UsuarioExtraUpdateComponent], + providers: [FormBuilder, TranslateService], + }) + .overrideTemplate(UsuarioExtraUpdateComponent, '') + .compileComponents(); + }) + ); beforeEach(() => { - TestBed.configureTestingModule({ - imports: [HttpClientTestingModule], - declarations: [UsuarioExtraUpdateComponent], - providers: [FormBuilder, ActivatedRoute], - }) - .overrideTemplate(UsuarioExtraUpdateComponent, '') - .compileComponents(); - fixture = TestBed.createComponent(UsuarioExtraUpdateComponent); - activatedRoute = TestBed.inject(ActivatedRoute); - usuarioExtraService = TestBed.inject(UsuarioExtraService); - userService = TestBed.inject(UserService); - plantillaService = TestBed.inject(PlantillaService); - comp = fixture.componentInstance; }); - describe('ngOnInit', () => { - it('Should call User query and add missing value', () => { - const usuarioExtra: IUsuarioExtra = { id: 456 }; - const user: IUser = { id: 58280 }; - usuarioExtra.user = user; - - const userCollection: IUser[] = [{ id: 29686 }]; - jest.spyOn(userService, 'query').mockReturnValue(of(new HttpResponse({ body: userCollection }))); - const additionalUsers = [user]; - const expectedCollection: IUser[] = [...additionalUsers, ...userCollection]; - jest.spyOn(userService, 'addUserToCollectionIfMissing').mockReturnValue(expectedCollection); - - activatedRoute.data = of({ usuarioExtra }); - comp.ngOnInit(); - - expect(userService.query).toHaveBeenCalled(); - expect(userService.addUserToCollectionIfMissing).toHaveBeenCalledWith(userCollection, ...additionalUsers); - expect(comp.usersSharedCollection).toEqual(expectedCollection); + it('should ensure the two passwords entered match', () => { + comp.registerForm.patchValue({ + password: 'password', + confirmPassword: 'non-matching', }); - it('Should call Plantilla query and add missing value', () => { - const usuarioExtra: IUsuarioExtra = { id: 456 }; - const plantillas: IPlantilla[] = [{ id: 54411 }]; - usuarioExtra.plantillas = plantillas; + comp.register(); - const plantillaCollection: IPlantilla[] = [{ id: 32212 }]; - jest.spyOn(plantillaService, 'query').mockReturnValue(of(new HttpResponse({ body: plantillaCollection }))); - const additionalPlantillas = [...plantillas]; - const expectedCollection: IPlantilla[] = [...additionalPlantillas, ...plantillaCollection]; - jest.spyOn(plantillaService, 'addPlantillaToCollectionIfMissing').mockReturnValue(expectedCollection); - - activatedRoute.data = of({ usuarioExtra }); - comp.ngOnInit(); - - expect(plantillaService.query).toHaveBeenCalled(); - expect(plantillaService.addPlantillaToCollectionIfMissing).toHaveBeenCalledWith(plantillaCollection, ...additionalPlantillas); - expect(comp.plantillasSharedCollection).toEqual(expectedCollection); - }); - - it('Should update editForm', () => { - const usuarioExtra: IUsuarioExtra = { id: 456 }; - const user: IUser = { id: 30429 }; - usuarioExtra.user = user; - const plantillas: IPlantilla = { id: 61011 }; - usuarioExtra.plantillas = [plantillas]; - - activatedRoute.data = of({ usuarioExtra }); - comp.ngOnInit(); - - expect(comp.editForm.value).toEqual(expect.objectContaining(usuarioExtra)); - expect(comp.usersSharedCollection).toContain(user); - expect(comp.plantillasSharedCollection).toContain(plantillas); - }); + expect(comp.doNotMatch).toBe(true); }); - describe('save', () => { - it('Should call update service on save for existing entity', () => { - // GIVEN - const saveSubject = new Subject>(); - const usuarioExtra = { id: 123 }; - jest.spyOn(usuarioExtraService, 'update').mockReturnValue(saveSubject); - jest.spyOn(comp, 'previousState'); - activatedRoute.data = of({ usuarioExtra }); - comp.ngOnInit(); - - // WHEN - comp.save(); - expect(comp.isSaving).toEqual(true); - saveSubject.next(new HttpResponse({ body: usuarioExtra })); - saveSubject.complete(); - - // THEN - expect(comp.previousState).toHaveBeenCalled(); - expect(usuarioExtraService.update).toHaveBeenCalledWith(usuarioExtra); - expect(comp.isSaving).toEqual(false); - }); - - it('Should call create service on save for new entity', () => { - // GIVEN - const saveSubject = new Subject>(); - const usuarioExtra = new UsuarioExtra(); - jest.spyOn(usuarioExtraService, 'create').mockReturnValue(saveSubject); - jest.spyOn(comp, 'previousState'); - activatedRoute.data = of({ usuarioExtra }); - comp.ngOnInit(); - - // WHEN - comp.save(); - expect(comp.isSaving).toEqual(true); - saveSubject.next(new HttpResponse({ body: usuarioExtra })); - saveSubject.complete(); - - // THEN - expect(usuarioExtraService.create).toHaveBeenCalledWith(usuarioExtra); - expect(comp.isSaving).toEqual(false); - expect(comp.previousState).toHaveBeenCalled(); - }); - - it('Should set isSaving to false on error', () => { - // GIVEN - const saveSubject = new Subject>(); - const usuarioExtra = { id: 123 }; - jest.spyOn(usuarioExtraService, 'update').mockReturnValue(saveSubject); - jest.spyOn(comp, 'previousState'); - activatedRoute.data = of({ usuarioExtra }); - comp.ngOnInit(); - - // WHEN - comp.save(); - expect(comp.isSaving).toEqual(true); - saveSubject.error('This is an error!'); - - // THEN - expect(usuarioExtraService.update).toHaveBeenCalledWith(usuarioExtra); - expect(comp.isSaving).toEqual(false); - expect(comp.previousState).not.toHaveBeenCalled(); - }); - }); - - describe('Tracking relationships identifiers', () => { - describe('trackUserById', () => { - it('Should return tracked User primary key', () => { - const entity = { id: 123 }; - const trackResult = comp.trackUserById(0, entity); - expect(trackResult).toEqual(entity.id); - }); - }); - - describe('trackPlantillaById', () => { - it('Should return tracked Plantilla primary key', () => { - const entity = { id: 123 }; - const trackResult = comp.trackPlantillaById(0, entity); - expect(trackResult).toEqual(entity.id); - }); - }); - }); - - describe('Getting selected relationships', () => { - describe('getSelectedPlantilla', () => { - it('Should return option if no Plantilla is selected', () => { - const option = { id: 123 }; - const result = comp.getSelectedPlantilla(option); - expect(result === option).toEqual(true); + it('should update success to true after creating an account', inject( + [RegisterService, TranslateService], + fakeAsync((service: RegisterService, mockLanguageService: TranslateService) => { + jest.spyOn(service, 'save').mockReturnValue(of({})); + mockLanguageService.currentLang = 'es'; + comp.registerForm.patchValue({ + password: 'password', + confirmPassword: 'password', }); - it('Should return selected Plantilla for according option', () => { - const option = { id: 123 }; - const selected = { id: 123 }; - const selected2 = { id: 456 }; - const result = comp.getSelectedPlantilla(option, [selected2, selected]); - expect(result === selected).toEqual(true); - expect(result === selected2).toEqual(false); - expect(result === option).toEqual(false); + comp.register(); + tick(); + + expect(service.save).toHaveBeenCalledWith({ + email: '', + password: 'password', + login: '', + langKey: 'es', + name: '', + profileIcon: 1, + isAdmin: 1, + }); + expect(comp.success).toBe(true); + expect(comp.errorUserExists).toBe(false); + expect(comp.errorEmailExists).toBe(false); + expect(comp.error).toBe(false); + }) + )); + + it('should notify of user existence upon 400/login already in use', inject( + [RegisterService], + fakeAsync((service: RegisterService) => { + jest.spyOn(service, 'save').mockReturnValue( + throwError({ + status: 400, + error: { type: LOGIN_ALREADY_USED_TYPE }, + }) + ); + comp.registerForm.patchValue({ + password: 'password', + confirmPassword: 'password', }); - it('Should return option if this Plantilla is not selected', () => { - const option = { id: 123 }; - const selected = { id: 456 }; - const result = comp.getSelectedPlantilla(option, [selected]); - expect(result === option).toEqual(true); - expect(result === selected).toEqual(false); + comp.register(); + tick(); + + expect(comp.errorUserExists).toBe(true); + expect(comp.errorEmailExists).toBe(false); + expect(comp.error).toBe(false); + }) + )); + + it('should notify of email existence upon 400/email address already in use', inject( + [RegisterService], + fakeAsync((service: RegisterService) => { + jest.spyOn(service, 'save').mockReturnValue( + throwError({ + status: 400, + error: { type: EMAIL_ALREADY_USED_TYPE }, + }) + ); + comp.registerForm.patchValue({ + password: 'password', + confirmPassword: 'password', }); - }); - }); + + comp.register(); + tick(); + + expect(comp.errorEmailExists).toBe(true); + expect(comp.errorUserExists).toBe(false); + expect(comp.error).toBe(false); + }) + )); + + it('should notify of generic error', inject( + [RegisterService], + fakeAsync((service: RegisterService) => { + jest.spyOn(service, 'save').mockReturnValue( + throwError({ + status: 503, + }) + ); + comp.registerForm.patchValue({ + password: 'password', + confirmPassword: 'password', + }); + + comp.register(); + tick(); + + expect(comp.errorUserExists).toBe(false); + expect(comp.errorEmailExists).toBe(false); + expect(comp.error).toBe(true); + }) + )); }); }); diff --git a/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.ts b/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.ts index b790e73..f712e2b 100644 --- a/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.ts +++ b/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.ts @@ -1,161 +1,121 @@ import { Component, OnInit } from '@angular/core'; -import { HttpResponse } from '@angular/common/http'; +import { HttpErrorResponse } from '@angular/common/http'; import { FormBuilder, Validators } from '@angular/forms'; -import { ActivatedRoute } from '@angular/router'; -import { Observable } from 'rxjs'; -import { finalize, map } from 'rxjs/operators'; +import { TranslateService } from '@ngx-translate/core'; -import * as dayjs from 'dayjs'; -import { DATE_TIME_FORMAT } from 'app/config/input.constants'; - -import { IUsuarioExtra, UsuarioExtra } from '../usuario-extra.model'; -import { UsuarioExtraService } from '../service/usuario-extra.service'; -import { IUser } from 'app/entities/user/user.model'; -import { UserService } from 'app/entities/user/user.service'; -import { IPlantilla } from 'app/entities/plantilla/plantilla.model'; -import { PlantillaService } from 'app/entities/plantilla/service/plantilla.service'; +import { EMAIL_ALREADY_USED_TYPE, LOGIN_ALREADY_USED_TYPE } from 'app/config/error.constants'; +import { RegisterService } from 'app/account/register/register.service'; @Component({ selector: 'jhi-usuario-extra-update', templateUrl: './usuario-extra-update.component.html', }) -export class UsuarioExtraUpdateComponent implements OnInit { - isSaving = false; +export class UsuarioExtraUpdateComponent { + // @ViewChild('name', { static: false }) + // name?: ElementRef; - usersSharedCollection: IUser[] = []; - plantillasSharedCollection: IPlantilla[] = []; + profileIcon: number = 1; + profileIcons: any[] = [ + { name: 'C1', class: 'active' }, + { name: 'C2' }, + { name: 'C3' }, + { name: 'C4' }, + { name: 'C5' }, + { name: 'C6' }, + { name: 'C7' }, + { name: 'C8' }, + { name: 'C9' }, + { name: 'C10' }, + { name: 'C11' }, + { name: 'C12' }, + { name: 'C13' }, + { name: 'C14' }, + { name: 'C15' }, + { name: 'C16' }, + { name: 'C17' }, + { name: 'C18' }, + { name: 'C19' }, + { name: 'C20' }, + { name: 'C21' }, + { name: 'C22' }, + { name: 'C23' }, + { name: 'C24' }, + { name: 'C25' }, + { name: 'C26' }, + { name: 'C27' }, + { name: 'C28' }, + ]; - editForm = this.fb.group({ - id: [], - nombre: [null, [Validators.required]], - iconoPerfil: [], - fechaNacimiento: [], - estado: [null, [Validators.required]], - user: [], - plantillas: [], + doNotMatch = false; + error = false; + errorEmailExists = false; + errorUserExists = false; + success = false; + + // Login will be used to store the email as well. + // login: ['', [Validators.required, Validators.minLength(5), Validators.maxLength(254), Validators.email]] + registerForm = this.fb.group({ + name: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(254)]], + email: ['', [Validators.required, Validators.minLength(5), Validators.maxLength(254), Validators.email]], + password: ['', [Validators.required, Validators.minLength(4), Validators.maxLength(50)]], + confirmPassword: ['', [Validators.required, Validators.minLength(4), Validators.maxLength(50)]], }); - constructor( - protected usuarioExtraService: UsuarioExtraService, - protected userService: UserService, - protected plantillaService: PlantillaService, - protected activatedRoute: ActivatedRoute, - protected fb: FormBuilder - ) {} + constructor(private translateService: TranslateService, private registerService: RegisterService, private fb: FormBuilder) {} - ngOnInit(): void { - this.activatedRoute.data.subscribe(({ usuarioExtra }) => { - if (usuarioExtra.id === undefined) { - const today = dayjs().startOf('day'); - usuarioExtra.fechaNacimiento = today; - } - - this.updateForm(usuarioExtra); - - this.loadRelationshipsOptions(); - }); + ngAfterViewInit(): void { + // if (this.name) { + // this.name.nativeElement.focus(); + // } } - previousState(): void { - window.history.back(); - } + register(): void { + this.doNotMatch = false; + this.error = false; + this.errorEmailExists = false; + this.errorUserExists = false; - save(): void { - this.isSaving = true; - const usuarioExtra = this.createFromForm(); - if (usuarioExtra.id !== undefined) { - this.subscribeToSaveResponse(this.usuarioExtraService.update(usuarioExtra)); + const password = this.registerForm.get(['password'])!.value; + if (password !== this.registerForm.get(['confirmPassword'])!.value) { + this.doNotMatch = true; } else { - this.subscribeToSaveResponse(this.usuarioExtraService.create(usuarioExtra)); + const login = this.registerForm.get(['email'])!.value; + const email = this.registerForm.get(['email'])!.value; + const name = this.registerForm.get(['name'])!.value; + console.log(name); + + this.registerService + .save({ + login, + email, + password, + langKey: this.translateService.currentLang, + name, + profileIcon: this.profileIcon, + isAdmin: 1, + }) + .subscribe( + () => (this.success = true), + response => this.processError(response) + ); } } - trackUserById(index: number, item: IUser): number { - return item.id!; - } - - trackPlantillaById(index: number, item: IPlantilla): number { - return item.id!; - } - - getSelectedPlantilla(option: IPlantilla, selectedVals?: IPlantilla[]): IPlantilla { - if (selectedVals) { - for (const selectedVal of selectedVals) { - if (option.id === selectedVal.id) { - return selectedVal; - } - } + private processError(response: HttpErrorResponse): void { + if (response.status === 400 && response.error.type === LOGIN_ALREADY_USED_TYPE) { + this.errorUserExists = true; + } else if (response.status === 400 && response.error.type === EMAIL_ALREADY_USED_TYPE) { + this.errorEmailExists = true; + } else { + this.error = true; } - return option; } - protected subscribeToSaveResponse(result: Observable>): void { - result.pipe(finalize(() => this.onSaveFinalize())).subscribe( - () => this.onSaveSuccess(), - () => this.onSaveError() - ); - } - - protected onSaveSuccess(): void { - this.previousState(); - } - - protected onSaveError(): void { - // Api for inheritance. - } - - protected onSaveFinalize(): void { - this.isSaving = false; - } - - protected updateForm(usuarioExtra: IUsuarioExtra): void { - this.editForm.patchValue({ - id: usuarioExtra.id, - nombre: usuarioExtra.nombre, - iconoPerfil: usuarioExtra.iconoPerfil, - fechaNacimiento: usuarioExtra.fechaNacimiento ? usuarioExtra.fechaNacimiento.format(DATE_TIME_FORMAT) : null, - estado: usuarioExtra.estado, - user: usuarioExtra.user, - plantillas: usuarioExtra.plantillas, - }); - - this.usersSharedCollection = this.userService.addUserToCollectionIfMissing(this.usersSharedCollection, usuarioExtra.user); - this.plantillasSharedCollection = this.plantillaService.addPlantillaToCollectionIfMissing( - this.plantillasSharedCollection, - ...(usuarioExtra.plantillas ?? []) - ); - } - - protected loadRelationshipsOptions(): void { - this.userService - .query() - .pipe(map((res: HttpResponse) => res.body ?? [])) - .pipe(map((users: IUser[]) => this.userService.addUserToCollectionIfMissing(users, this.editForm.get('user')!.value))) - .subscribe((users: IUser[]) => (this.usersSharedCollection = users)); - - this.plantillaService - .query() - .pipe(map((res: HttpResponse) => res.body ?? [])) - .pipe( - map((plantillas: IPlantilla[]) => - this.plantillaService.addPlantillaToCollectionIfMissing(plantillas, ...(this.editForm.get('plantillas')!.value ?? [])) - ) - ) - .subscribe((plantillas: IPlantilla[]) => (this.plantillasSharedCollection = plantillas)); - } - - protected createFromForm(): IUsuarioExtra { - return { - ...new UsuarioExtra(), - id: this.editForm.get(['id'])!.value, - nombre: this.editForm.get(['nombre'])!.value, - iconoPerfil: this.editForm.get(['iconoPerfil'])!.value, - fechaNacimiento: this.editForm.get(['fechaNacimiento'])!.value - ? dayjs(this.editForm.get(['fechaNacimiento'])!.value, DATE_TIME_FORMAT) - : undefined, - estado: this.editForm.get(['estado'])!.value, - user: this.editForm.get(['user'])!.value, - plantillas: this.editForm.get(['plantillas'])!.value, - }; + selectIcon(event: MouseEvent): void { + if (event.target instanceof Element) { + document.querySelectorAll('.active').forEach(e => e.classList.remove('active')); + event.target.classList.add('active'); + this.profileIcon = +event.target.getAttribute('id')! + 1; + } } } diff --git a/src/main/webapp/app/entities/usuario-extra/usuario-extra.module.ts b/src/main/webapp/app/entities/usuario-extra/usuario-extra.module.ts index beab505..1abd9c6 100644 --- a/src/main/webapp/app/entities/usuario-extra/usuario-extra.module.ts +++ b/src/main/webapp/app/entities/usuario-extra/usuario-extra.module.ts @@ -5,9 +5,10 @@ import { UsuarioExtraDetailComponent } from './detail/usuario-extra-detail.compo import { UsuarioExtraUpdateComponent } from './update/usuario-extra-update.component'; import { UsuarioExtraDeleteDialogComponent } from './delete/usuario-extra-delete-dialog.component'; import { UsuarioExtraRoutingModule } from './route/usuario-extra-routing.module'; +import { ComponentsModule } from 'app/components/components.module'; @NgModule({ - imports: [SharedModule, UsuarioExtraRoutingModule], + imports: [SharedModule, UsuarioExtraRoutingModule, ComponentsModule], declarations: [UsuarioExtraComponent, UsuarioExtraDetailComponent, UsuarioExtraUpdateComponent, UsuarioExtraDeleteDialogComponent], entryComponents: [UsuarioExtraDeleteDialogComponent], }) diff --git a/src/main/webapp/i18n/es/register.json b/src/main/webapp/i18n/es/register.json index 9a44d61..37bc69b 100644 --- a/src/main/webapp/i18n/es/register.json +++ b/src/main/webapp/i18n/es/register.json @@ -14,6 +14,7 @@ } }, "success": "¡Registro guardado! Por favor, revise su correo electrónico para confirmar.", + "adminsuccess": "¡Registro guardado! Por favor, revise el correo electrónico para confirmar.", "error": { "fail": "¡El registro ha fallado! Por favor, inténtelo de nuevo más tarde.", "userexists": "¡El nombre de usuario ya está registrado! Por favor, escoja otro usuario.", From db2b5f6fad167e4ff14a9357bcdaf07e786e5c59 Mon Sep 17 00:00:00 2001 From: Pablo Bonilla Date: Tue, 6 Jul 2021 23:36:40 -0600 Subject: [PATCH 3/3] Update register administrator html class --- .../usuario-extra/update/usuario-extra-update.component.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html b/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html index 9c332db..d75c700 100644 --- a/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html +++ b/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html @@ -1,6 +1,6 @@
-
+