регистрирую нового пользователя
public void registerNewUser(ActionEvent actionEvent) throws Exception {
if (!isCustomerExist()) {
Customer customer = customerRepository.createCustomer(login, password);
FacesUtils.putUserId(customer.getId());
if (firstPage.isCreateAsAdmin()) {
redirectToAdmin();
} else {
redirectToOverview();
}
}
else{ FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Customer already exits"));}
}
метод должен проверить роль пользователя
public Customer createCustomer(String login, String password) throws Exception {
return this.createCustomer(login, password, roleRepository.getCustomerRole());// здесь ноль выдает
}
RoleRepository roleRepository ;
private Customer createCustomer(String login, String password, Role role) {
Customer customer = null;
try{
customer = new Customer();
customer.setLogin(login);
customer.setPassword(password);
customer.addRole(role);
entityManager.persist(customer);
}
catch (Exception e){
System.out.println(e.toString());
}
return customer;
}
сами роли
public Role createRole(String name) {
Role role = new Role();
role.setRollName(name);
entityManager.persist(role);
return role;
}
public Role findByRole(String roleName) throws EntityNotFoundException {
Role role = find(Role.class, "Role.findByRole", roleName);
if (Objects.isNull(role)) {
throw new EntityNotFoundException("Could not find role with specified name " + roleName);
}
return role;
}
public Role getCustomerRole() throws Exception {
try {
Role role = findByRole("user");
System.out.println("Role nicht gefunden");
return role;
} catch (EntityNotFoundException e) {
System.out.println("EntityNotFoundException ");
return createRole("user");
}
}
public Role getAdminRole() {
try {
Role role = findByRole("admin");
return role;
} catch (EntityNotFoundException e) {
return createRole("admin");
}
}
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости