Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing link creation Local Users - Institutions #14

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/configuration/CUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ public class CUser {
private String password;
private Boolean active = true;
private List<String> roles;
private List<String> institutions;

public CUser() {
}

public CUser(final String name, final String email, final String password,
final Boolean active, final List<String> roles) {
final Boolean active, final List<String> roles, final List<String> institutions) {
this.name = name;
this.email = email;
this.password = password;
this.active = active;
this.roles = roles;
this.institutions = institutions;
}

public String getName() {
Expand Down Expand Up @@ -64,6 +66,14 @@ public void setRoles(final List<String> roles) {
this.roles = roles;
}

public List<String> getInstitutions() {
return institutions;
}

public void setInstitutions(List<String> institutions) {
this.institutions = institutions;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/InstitutionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public InstitutionController(InstitutionRepository institutionRepository, Server
this.serverRepository = serverRepository;
this.dataSourceRepository = dataSourceRepository;
this.componentRepository = componentRepository;
this.componentCategoryRepository = componentCategoryRepository;
this.userRepository = userRepository;
}

this.componentCategoryRepository = componentCategoryRepository;
this.userRepository = userRepository;
}
@Security.Authenticated(Secured.class)
@CheckPermission(category = Category.INSTITUTION, needs = {Operation.ADD})
public Result newInstitution() {
Expand Down
11 changes: 11 additions & 0 deletions app/repositories/JPAInstitutionRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public static Institution getByName(EntityManager em, String name) {
.getSingleResult();
}

public Institution findInstitution(EntityManager em, String name) {
try {
return em.createQuery("SELECT p FROM Institution p WHERE p.name=:name", Institution.class)
.setParameter("name", name)
.setMaxResults(1)
.getSingleResult();
} catch (NoResultException e) {
return null;
}
}

public static boolean hasUser(EntityManager em, long institutionId, String userEmail) {
try {
return em
Expand Down
14 changes: 7 additions & 7 deletions app/services/ApplicationStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ public ApplicationStart(ComponentCategoryRepository componentCategoryRepository,
// Building Step Configurations.
buildComponents(configuration);

// Building users
boolean initRBAC = rbacRepository.findAllRoles().size() == 0;
if (initRBAC) {
buildRBAC(configuration);
}

// Building Institutions.
boolean initInstitutions = this.institutionRepository.list().size() == 0;
if (initInstitutions) {
Expand All @@ -98,6 +92,12 @@ public ApplicationStart(ComponentCategoryRepository componentCategoryRepository,
}
}

// Building users
boolean initRBAC = rbacRepository.findAllRoles().size() == 0;
if (initRBAC) {
buildRBAC(configuration);
}

boolean initAuth = authenticationRepository.findAll().size() == 0;
if (initAuth) {
try {
Expand Down Expand Up @@ -158,7 +158,7 @@ private void buildRBAC(Configuration configuration) {
User user = new User(u.getEmail(), u.getName(), BCrypt.hashpw(u.getPassword(), BCrypt.gensalt()));
user.setActive(u.getActive());

userService.createUserWithRoles(user, u.getRoles());
userService.createUserWithRolesAndInstitutions(user, u.getRoles(), u.getInstitutions());
}
}

Expand Down
131 changes: 73 additions & 58 deletions app/services/rbac/UserService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package services.rbac;

import com.google.inject.Inject;
import models.Institution;
import models.rbac.Role;
import models.rbac.User;
import org.mindrot.jbcrypt.BCrypt;
import play.db.jpa.JPAApi;
import repositories.JPAInstitutionRepository;
import repositories.user.JPARBACRepository;
import repositories.user.JPAUserRepository;
import services.JPAService;
Expand All @@ -13,63 +15,76 @@
import java.util.List;

public class UserService extends JPAService {
private final JPAUserRepository userRepository;
private final JPARBACRepository rbacRepository;

@Inject
public UserService(JPAApi jpaApi, JPAUserRepository userRepository, JPARBACRepository rbacRepository) {
super(jpaApi);
this.userRepository = userRepository;
this.rbacRepository = rbacRepository;
}

public User get(long id) {
return userRepository.get(id);
}

public User findByEmail(String email) {
return userRepository.findByEmail(email);
}

public User authenticate(String username, String password) {
try {
User user = userRepository.findByEmail(username);

// Only authenticate local users
if (user == null || user.getType() != User.UserType.Local) {
return null;
}

if (BCrypt.checkpw(password, user.getPassword())) {
return user;
}

return null;
} catch (Exception ex) {
// No user found, returns null
return null;
}
}

public User createUserWithRoles(User user, List<String> roles) {
return withTransaction(em -> {
List<Role> _roles = new ArrayList<>();

for (String r : roles) {
Role role = rbacRepository.findRole(em, r);
if (role == null) {
System.err.println("User role not found: " + r);
continue;
}

_roles.add(role);
}

user.getRoles().addAll(_roles);
userRepository.create(em, user);

return user;
});
}
private final JPAUserRepository userRepository;
private final JPARBACRepository rbacRepository;
private final JPAInstitutionRepository institutionRepository;

@Inject
public UserService(JPAApi jpaApi, JPAUserRepository userRepository, JPARBACRepository rbacRepository, JPAInstitutionRepository institutionRepository) {
super(jpaApi);
this.userRepository = userRepository;
this.rbacRepository = rbacRepository;
this.institutionRepository = institutionRepository;
}

public User get(long id) {
return userRepository.get(id);
}

public User findByEmail(String email) {
return userRepository.findByEmail(email);
}

public User authenticate(String username, String password) {
try {
User user = userRepository.findByEmail(username);

// Only authenticate local users
if (user == null || user.getType() != User.UserType.Local) {
return null;
}

if (BCrypt.checkpw(password, user.getPassword())) {
return user;
}

return null;
} catch (Exception ex) {
// No user found, returns null
return null;
}
}

public User createUserWithRolesAndInstitutions(User user, List<String> roles, List<String> institutions) {
return withTransaction(em -> {
List<Role> _roles = new ArrayList<>();
List<Institution> _institutions = new ArrayList<>();

for (String r : roles) {
Role role = rbacRepository.findRole(em, r);
if (role == null) {
System.err.println("User role not found: " + r);
continue;
}
_roles.add(role);
}

for (String r : institutions) {
Institution institution = institutionRepository.findInstitution(em, r);
if (institution == null) {
System.err.println("User institutions not found: " + r);
continue;
}
_institutions.add(institution);
}

user.getRoles().addAll(_roles);
user.getInstitutions().addAll(_institutions);

userRepository.create(em, user);

return user;
});
}

}
4 changes: 4 additions & 0 deletions conf/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,10 @@
"active": true,
"roles": [
"Administrator"
],
"institutions": [
"INSA",
"Briosa e Gala"
]
}
],
Expand Down