Package ch.inftec.ju.db.auth.repo

Examples of ch.inftec.ju.db.auth.repo.AuthUserRepo


   * @param user Existing user
   * @param roleName Role name
   */
  public void addRole(AuthUser user, String roleName) {
    AuthRoleRepo roleRepo = JuDbUtils.getJpaRepository(this.em, AuthRoleRepo.class);
    AuthUserRepo userRepo = JuDbUtils.getJpaRepository(this.em, AuthUserRepo.class);
   
    // Check if the role exists
    AuthRole role = roleRepo.getByName(roleName);
    if (role == null) {
      role = new AuthRole();
      role.setName(roleName);
      this.em.persist(role);
    }
   
    // Check if the role has already been assigned to the user
    if (roleRepo.getByNameAndUsersId(roleName, user.getId()) == null) {
      // Role hasn't been assigned, so do it
      user.getRoles().add(role);
      role.getUsers().add(user);
      userRepo.save(user);
      roleRepo.save(role);
    }
  }
View Full Code Here


   * @param user
   * @param roleName
   */
  public void removeRole(AuthUser user, String roleName) {
    AuthRoleRepo roleRepo = JuDbUtils.getJpaRepository(this.em, AuthRoleRepo.class);
    AuthUserRepo userRepo = JuDbUtils.getJpaRepository(this.em, AuthUserRepo.class);
   
    // Check if the role exists
    AuthRole role = roleRepo.getByName(roleName);
    if (role != null) {
      if (roleRepo.getByNameAndUsersId(roleName, user.getId()) != null) {
        // User has role assigned, so remove it
        role.getUsers().remove(user);
        user.getRoles().remove(role);
        roleRepo.save(role);
        userRepo.save(user);
      }
    }
  }
View Full Code Here

TOP

Related Classes of ch.inftec.ju.db.auth.repo.AuthUserRepo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.