use of com.autentia.tnt.manager.security.Principal in project TNTConcept by autentia.
the class ActivityEvidenceNotificationBean_IT method insertActivityWithRequiredEvidence.
private void insertActivityWithRequiredEvidence(User user) {
ProjectRole role = ((ProjectRoleDAO) SpringUtils.getSpringBean("daoProjectRole")).getById(3);
Activity activity = new Activity();
activity.setDescription("Test activity");
activity.setHasImage(false);
activity.setStartDate(Date.from(LocalDate.now().plusDays(-4).atStartOfDay(ZoneId.systemDefault()).toInstant()));
activity.setRole(role);
// Even if user is set on activity, if completely ignores it, so...
final Principal principal = (Principal) AuthenticationManager.getDefault().loadUserByUsername(user.getLogin());
Authentication auth = new UsernamePasswordAuthenticationToken(principal, principal.getUser().getPassword(), principal.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(auth);
ActivityDAO activityDAO = (ActivityDAO) SpringUtils.getSpringBean("daoActivity");
activityDAO.insert(activity);
}
use of com.autentia.tnt.manager.security.Principal in project TNTConcept by autentia.
the class ActivityEvidenceNotificationBean_IT method insertActivityWithNonRequiredEvidence.
private void insertActivityWithNonRequiredEvidence(User user) {
ProjectRole role = ((ProjectRoleDAO) SpringUtils.getSpringBean("daoProjectRole")).getById(1);
Activity activity = new Activity();
activity.setDescription("Test activity 2");
activity.setHasImage(true);
activity.setStartDate(Date.from(LocalDate.now().plusDays(-4).atStartOfDay(ZoneId.systemDefault()).toInstant()));
activity.setRole(role);
// Even if user is set on activity, if completely ignores it, so...
final Principal principal = (Principal) AuthenticationManager.getDefault().loadUserByUsername(user.getLogin());
Authentication auth = new UsernamePasswordAuthenticationToken(principal, principal.getUser().getPassword(), principal.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(auth);
ActivityDAO activityDAO = (ActivityDAO) SpringUtils.getSpringBean("daoActivity");
activityDAO.insertWithoutUser(activity);
}
use of com.autentia.tnt.manager.security.Principal in project TNTConcept by autentia.
the class SpringUtilsForTesting method setUserForTestingInContext.
private static UserForTesting setUserForTestingInContext() {
final GrantedAuthority[] authorities = new GrantedAuthority[] { new GrantedAuthorityImpl("User"), new GrantedAuthorityImpl("Administrator") };
final UserForTesting user = new UserForTesting();
user.setLogin("admin");
user.setId(1);
final DepartmentForTesting departmentForTesting = new DepartmentForTesting();
departmentForTesting.setId(1);
user.setDepartment(departmentForTesting);
final RoleForTesting roleForTesting = new RoleForTesting();
roleForTesting.setId(1);
user.setRole(roleForTesting);
final Principal principal = new Principal(user, authorities);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(principal, "admin"));
return user;
}
use of com.autentia.tnt.manager.security.Principal in project TNTConcept by autentia.
the class SpringUtils method isRolePermissionGranted.
/**
* Test if a given role permission is granted to current user.
* @param grantedAuthority the permission to test
* @return true if current user holds permission
*/
public static boolean isRolePermissionGranted(GrantedAuthority perm) {
boolean isGranted = true;
try {
Principal principal = getPrincipal();
isGranted = principal.hasAuthority(perm);
} catch (Exception nfe) {
isGranted = false;
}
return isGranted;
}
use of com.autentia.tnt.manager.security.Principal in project TNTConcept by autentia.
the class SpringUtilsForTesting method loadPrincipalInSecurityContext.
public static void loadPrincipalInSecurityContext(String username) {
AuthenticationManager authManager = (AuthenticationManager) getSpringBean("userDetailsService");
Principal principal = (Principal) authManager.loadUserByUsername(username);
Authentication auth = new UsernamePasswordAuthenticationToken(principal, principal.getUser().getPassword(), principal.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(auth);
}
Aggregations