Skip to content

Commit

Permalink
Issue #171
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph-Meyer committed Jan 9, 2019
1 parent a05e1fd commit f0a8efd
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Permission> permissions = permissionService.getAccessPermissions(user);

final List<SimpleGrantedAuthority> 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<Permission> permissions = permissionService.getAccessPermissions(user);

final List<SimpleGrantedAuthority> 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<User> findAll() {
return userRepository.findAll();
}
public User findById(final long userId) {
return userRepository.findOne(userId);
}

public List<User> findByRole(final RoleName roleName) {
return findAll().stream().filter(user -> user.getRole().getName() == roleName).collect(Collectors.toList());
}
public List<User> findAll() {
return userRepository.findAll();
}

public Level getLevel(final long xp) {
return levelService.getLevelByUserXp(xp);
}
public List<User> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ <h2 mat-dialog-title>{{'ADMIN_PAGE.CREATE_DEVELOPER' | translate}}</h2>
<div layout="column" layout-padding>
<div flex>
<mat-input-container class="full-width">
<input class="form-control" formControlName="name" matInput placeholder="Username">
<input class="form-control" formControlName="name" matInput placeholder="{{'DEVELOPER.DEVELOPER' | translate}}">
<mat-error>{{getErrorMessage()}}</mat-error>
</mat-input-container>
</div>
<div flex>
<mat-input-container class="full-width">
<input class="form-control" formControlName="password" matInput placeholder="Password" [type]="'password'">
<input class="form-control" formControlName="password" matInput placeholder="{{'LOGIN.PASSWORD' | translate}}" [type]="'password'">
<mat-error>{{getErrorMessage()}}</mat-error>
</mat-input-container>
</div>
<div flex>
<mat-input-container class="full-width">
<textarea class="form-control" formControlName="about" matInput placeholder="AboutMe"></textarea>
<textarea class="form-control" formControlName="about" matInput placeholder="{{'DEVELOPER.ABOUTME' | translate}}"></textarea>
</mat-input-container>
</div>
<div flex>
<mat-select placeholder="Role" matInput formControlName="role">
<mat-select placeholder="{{'TABLE.COLUMNS.ROLE' | translate}}" matInput formControlName="role">
<mat-option *ngFor="let role of roles" [value]="role">
{{role.name}}
</mat-option>
<mat-error>{{getErrorMessage()}}</mat-error>
</mat-select>
</div>
<div flex>
<mat-select placeholder="Race" matInput formControlName="race">
<mat-select placeholder="{{'DEVELOPER.AVATARRACE' | translate}}" matInput formControlName="race">
<mat-option *ngFor="let race of races" [value]="race">
{{race.name}}
</mat-option>
<mat-error>{{getErrorMessage()}}</mat-error>
</mat-select>
</div>
<div flex>
<mat-select placeholder="Class" matInput formControlName="class">
<mat-select placeholder="{{'DEVELOPER.AVATARCLASS' | translate}}" matInput formControlName="class">
<mat-option *ngFor="let class of classes" [value]="class">
{{class.name}}
</mat-option>
Expand All @@ -46,7 +46,7 @@ <h2 mat-dialog-title>{{'ADMIN_PAGE.CREATE_DEVELOPER' | translate}}</h2>
</div>
<div flex>
<mat-input-container class="full-width">
<input class="form-control" formControlName="picture" matInput placeholder="File name">
<input class="form-control" formControlName="picture" matInput placeholder="{{'GLOBAL.FILENAME_IMAGE' | translate}}">
</mat-input-container>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ <h2 mat-dialog-title>{{'ADMIN_PAGE.EDIT_DEVELOPER' | translate}}</h2>
<div layout="column" layout-padding>
<div flex>
<mat-input-container class="full-width">
<input matInput placeholder="Username" [(ngModel)]="user.username">
<input matInput placeholder="{{'DEVELOPER.DEVELOPER' | translate}}" [(ngModel)]="user.username">
</mat-input-container>
</div>
<div flex>
<mat-input-container class="full-width">
<textarea matInput placeholder="AboutMe" [(ngModel)]="user.aboutMe"></textarea>
<textarea matInput placeholder="{{'DEVELOPER.ABOUTME' | translate}}" [(ngModel)]="user.aboutMe"></textarea>
</mat-input-container>
</div>
<div flex>
<mat-select placeholder="{{'TABLE.COLUMNS.ROLE' | translate}}" matInput [(ngModel)]="user.role">
<mat-option *ngFor="let role of roles" [value]="role">
{{role.name}}
</mat-option>
</mat-select>
</div>
<hr/>
<div>
<img [src]="imageToShow">
</div>
<div>
<mat-input-container class="full-width">
<input matInput placeholder="File name" [(ngModel)]="user.picture">
<input matInput placeholder="{{'GLOBAL.FILENAME_IMAGE' | translate}}" [(ngModel)]="user.picture">
</mat-input-container>
</div>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -20,6 +21,7 @@ export class AdminDeveloperEditComponent implements OnInit {

imageToShow: any;
userToWorlds: UserToWorld[];
roles: Role[];

columns: ITdDataTableColumn[] = [
{ name: 'userId', label: 'UserId', hidden: true },
Expand All @@ -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
) {
}
Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions sonarQuest-frontend/src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions sonarQuest-frontend/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f0a8efd

Please sign in to comment.