From f0a8efd9aae078c5d91b8db73b3e3621beca89c5 Mon Sep 17 00:00:00 2001 From: Christoph-Meyer Date: Wed, 9 Jan 2019 19:15:19 +0100 Subject: [PATCH] Issue #171 --- .../sonarquest/services/UserService.java | 222 +++++++++--------- .../admin-developer-create.component.html | 14 +- .../admin-developer-edit.component.html | 14 +- .../admin-developer-edit.component.ts | 6 +- sonarQuest-frontend/src/assets/i18n/de.json | 1 + sonarQuest-frontend/src/assets/i18n/en.json | 1 + 6 files changed, 138 insertions(+), 120 deletions(-) diff --git a/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/UserService.java b/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/UserService.java index 81cfdeb3..3c4d8369 100644 --- a/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/UserService.java +++ b/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/UserService.java @@ -25,123 +25,127 @@ @Service public class UserService implements UserDetailsService { - private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class); - - @Autowired - private RoleService roleService; - - @Autowired - private UserRepository userRepository; - - @Autowired - private WorldService worldService; - - @Autowired - private LevelService levelService; - - @Autowired - private PermissionService permissionService; - - @Override - public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException { - final User user = findByUsername(username); - final Set permissions = permissionService.getAccessPermissions(user); - - final List authoritys = permissions.stream() - .map(berechtigung -> new SimpleGrantedAuthority(berechtigung.getPermission())) - .collect(Collectors.toList()); - - return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), true, - true, true, true, authoritys); - } - - public User findByUsername(final String username) { - return userRepository.findByUsername(username); - } - - private User findById(final Long id) { - return userRepository.findOne(id); - } - - public World updateUsersCurrentWorld(final User user, final Long worldId) { - final World world = worldService.findById(worldId); - user.setCurrentWorld(world); - userRepository.saveAndFlush(user); - return user.getCurrentWorld(); - } - - public synchronized User save(final User user) { - User toBeSaved = null; - String username = user.getUsername(); - BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); - if (user.getId() == null) { - // Only the password hash needs to be saved + private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class); + + @Autowired + private RoleService roleService; + + @Autowired + private UserRepository userRepository; + + @Autowired + private WorldService worldService; + + @Autowired + private LevelService levelService; + + @Autowired + private PermissionService permissionService; + + @Override + public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException { + final User user = findByUsername(username); + final Set permissions = permissionService.getAccessPermissions(user); + + final List authoritys = permissions.stream() + .map(berechtigung -> new SimpleGrantedAuthority(berechtigung.getPermission())) + .collect(Collectors.toList()); + + return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), true, + true, true, true, authoritys); + } + + public User findByUsername(final String username) { + return userRepository.findByUsername(username); + } + + private User findById(final Long id) { + return userRepository.findOne(id); + } + + public World updateUsersCurrentWorld(final User user, final Long worldId) { + final World world = worldService.findById(worldId); + user.setCurrentWorld(world); + userRepository.saveAndFlush(user); + return user.getCurrentWorld(); + } + + public synchronized User save(final User user) { + User toBeSaved = null; + String username = user.getUsername(); + BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); + if (user.getId() == null) { + // Only the password hash needs to be saved + String password = encoder.encode(user.getPassword()); + Role role = user.getRole(); + RoleName roleName = role.getName(); + Role userRole = roleService.findByName(roleName); + toBeSaved = usernameFree(username) ? user : null; + if (toBeSaved != null) { + toBeSaved.setPassword(password); + toBeSaved.setRole(userRole); + toBeSaved.setCurrentWorld(user.getCurrentWorld()); + toBeSaved.setGold(0l); + toBeSaved.setXp(0l); + toBeSaved.setLevel(levelService.getLevelByUserXp(0l)); + } + } else { + toBeSaved = findById(user.getId()); + if (toBeSaved != null) { + if (!username.equals(toBeSaved.getUsername()) && usernameFree(username)) { + toBeSaved.setUsername(username); + } + // if there are identical hashes in the pw fields, do not touch them + if (!toBeSaved.getPassword().equals(user.getPassword())) { + // change password only if it differs from the old one + String oldPassHash = toBeSaved.getPassword(); + if (!oldPassHash.equals(user.getPassword()) || !encoder.matches(user.getPassword(), oldPassHash)) { String password = encoder.encode(user.getPassword()); - Role role = user.getRole(); - RoleName roleName = role.getName(); - Role userRole = roleService.findByName(roleName); - toBeSaved = usernameFree(username) ? user : null; - if (toBeSaved != null) { - toBeSaved.setPassword(password); - toBeSaved.setRole(userRole); - toBeSaved.setCurrentWorld(user.getCurrentWorld()); - toBeSaved.setGold(0l); - toBeSaved.setXp(0l); - toBeSaved.setLevel(levelService.getLevelByUserXp(0l)); - } - } else { - toBeSaved = findById(user.getId()); - if (toBeSaved != null) { - if (!username.equals(toBeSaved.getUsername()) && usernameFree(username)) { - toBeSaved.setUsername(username); - } - // if there are identical hashes in the pw fields, do not touch them - if (!toBeSaved.getPassword().equals(user.getPassword())) { - // change password only if it differs from the old one - String oldPassHash = toBeSaved.getPassword(); - if (!oldPassHash.equals(user.getPassword()) || !encoder.matches(user.getPassword(), oldPassHash)) { - String password = encoder.encode(user.getPassword()); - toBeSaved.setPassword(password); - LOGGER.info("The password for user " + user.getUsername() + " (id: " + user.getId() - + ") has been changed."); - } - } - toBeSaved.setAboutMe(user.getAboutMe()); - toBeSaved.setPicture(user.getPicture()); - toBeSaved.setCurrentWorld(user.getCurrentWorld()); - toBeSaved.setWorlds(user.getWorlds()); - toBeSaved.setGold(user.getGold()); - toBeSaved.setXp(user.getXp()); - toBeSaved.setLevel(levelService.getLevelByUserXp(user.getXp())); - } + toBeSaved.setPassword(password); + LOGGER.info("The password for user " + user.getUsername() + " (id: " + user.getId() + + ") has been changed."); + } } - - return toBeSaved != null ? userRepository.saveAndFlush(toBeSaved) : null; + Role role = user.getRole(); + RoleName roleName = role.getName(); + Role userRole = roleService.findByName(roleName); + toBeSaved.setRole(userRole); + toBeSaved.setAboutMe(user.getAboutMe()); + toBeSaved.setPicture(user.getPicture()); + toBeSaved.setCurrentWorld(user.getCurrentWorld()); + toBeSaved.setWorlds(user.getWorlds()); + toBeSaved.setGold(user.getGold()); + toBeSaved.setXp(user.getXp()); + toBeSaved.setLevel(levelService.getLevelByUserXp(user.getXp())); + } } - private boolean usernameFree(final String username) { - return userRepository.findByUsername(username) == null; - } + return toBeSaved != null ? userRepository.saveAndFlush(toBeSaved) : null; + } - public void delete(final Long userId) { - final User user = findById(userId); - userRepository.delete(user); - } + private boolean usernameFree(final String username) { + return userRepository.findByUsername(username) == null; + } - public User findById(final long userId) { - return userRepository.findOne(userId); - } + public void delete(final Long userId) { + final User user = findById(userId); + userRepository.delete(user); + } - public List findAll() { - return userRepository.findAll(); - } + public User findById(final long userId) { + return userRepository.findOne(userId); + } - public List findByRole(final RoleName roleName) { - return findAll().stream().filter(user -> user.getRole().getName() == roleName).collect(Collectors.toList()); - } + public List findAll() { + return userRepository.findAll(); + } - public Level getLevel(final long xp) { - return levelService.getLevelByUserXp(xp); - } + public List findByRole(final RoleName roleName) { + return findAll().stream().filter(user -> user.getRole().getName() == roleName).collect(Collectors.toList()); + } + + public Level getLevel(final long xp) { + return levelService.getLevelByUserXp(xp); + } } diff --git a/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-create/admin-developer-create.component.html b/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-create/admin-developer-create.component.html index 3851df9d..067e7b90 100644 --- a/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-create/admin-developer-create.component.html +++ b/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-create/admin-developer-create.component.html @@ -5,23 +5,23 @@

{{'ADMIN_PAGE.CREATE_DEVELOPER' | translate}}

- + {{getErrorMessage()}}
- + {{getErrorMessage()}}
- +
- + {{role.name}} @@ -29,7 +29,7 @@

{{'ADMIN_PAGE.CREATE_DEVELOPER' | translate}}

- + {{race.name}} @@ -37,7 +37,7 @@

{{'ADMIN_PAGE.CREATE_DEVELOPER' | translate}}

- + {{class.name}} @@ -46,7 +46,7 @@

{{'ADMIN_PAGE.CREATE_DEVELOPER' | translate}}

- +
diff --git a/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.html b/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.html index eeb984f4..a8e6290b 100644 --- a/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.html +++ b/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.html @@ -3,20 +3,28 @@

{{'ADMIN_PAGE.EDIT_DEVELOPER' | translate}}

- +
- +
+
+ + + {{role.name}} + + +
+
- +
diff --git a/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.ts b/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.ts index 2fc0f2b4..6802a05e 100644 --- a/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.ts +++ b/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.ts @@ -6,10 +6,11 @@ import { Component, OnInit, Inject } from '@angular/core'; import { UserService } from '../../../../../../services/user.service'; import { User } from '../../../../../../Interfaces/User'; import { ImageService } from '../../../../../../services/image.service'; -import { WorldService } from '../../../../../../services/world.service'; import { UserToWorld } from '../../../../../../Interfaces/UserToWorld'; import { ITdDataTableColumn } from '@covalent/core'; import { UserToWorldService } from '../../../../../../services/user-to-world.service'; +import { Role } from 'app/Interfaces/Role'; +import { RoleService } from 'app/services/role.service'; @Component({ selector: 'app-admin-developer-edit', @@ -20,6 +21,7 @@ export class AdminDeveloperEditComponent implements OnInit { imageToShow: any; userToWorlds: UserToWorld[]; + roles: Role[]; columns: ITdDataTableColumn[] = [ { name: 'userId', label: 'UserId', hidden: true }, @@ -34,6 +36,7 @@ export class AdminDeveloperEditComponent implements OnInit { private translateService: TranslateService, @Inject(MAT_DIALOG_DATA) public user: User, private imageService: ImageService, + private roleService: RoleService, private userToWorldService: UserToWorldService ) { } @@ -42,6 +45,7 @@ export class AdminDeveloperEditComponent implements OnInit { this.translateTable(); this.loadImages(); this.userToWorldService.getUserToWorlds(this.user).then(userToWorlds => this.userToWorlds = userToWorlds); + this.roleService.getRoles().then(roles => this.roles = roles); } translateTable() { diff --git a/sonarQuest-frontend/src/assets/i18n/de.json b/sonarQuest-frontend/src/assets/i18n/de.json index 695c66bc..3ff7a0d8 100644 --- a/sonarQuest-frontend/src/assets/i18n/de.json +++ b/sonarQuest-frontend/src/assets/i18n/de.json @@ -124,6 +124,7 @@ "SAVE": "Speichern", "SOLVEALLTASKS" : "Alle Aufgaben lösen", "IMAGE": "Bild", + "FILENAME_IMAGE": "Dateiname Bild", "CLICK_TO_SELECT": "Klick zum Wählen", "GOLD": "Gold", "XP": "XP", diff --git a/sonarQuest-frontend/src/assets/i18n/en.json b/sonarQuest-frontend/src/assets/i18n/en.json index f1cdcaf7..33fba3ad 100644 --- a/sonarQuest-frontend/src/assets/i18n/en.json +++ b/sonarQuest-frontend/src/assets/i18n/en.json @@ -124,6 +124,7 @@ "SAVE": "Save", "SOLVEALLTASKS" : "Solve all tasks", "IMAGE": "Image", + "FILENAME_IMAGE": "Filename image", "CLICK_TO_SELECT": "clickt to select", "GOLD": "Gold", "XP": "XP",