Search in sources :

Example 6 with Principal

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);
}
Also used : Authentication(org.acegisecurity.Authentication) UsernamePasswordAuthenticationToken(org.acegisecurity.providers.UsernamePasswordAuthenticationToken) Principal(com.autentia.tnt.manager.security.Principal)

Example 7 with Principal

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);
}
Also used : Authentication(org.acegisecurity.Authentication) UsernamePasswordAuthenticationToken(org.acegisecurity.providers.UsernamePasswordAuthenticationToken) Principal(com.autentia.tnt.manager.security.Principal)

Example 8 with Principal

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;
}
Also used : GrantedAuthorityImpl(org.acegisecurity.GrantedAuthorityImpl) GrantedAuthority(org.acegisecurity.GrantedAuthority) UsernamePasswordAuthenticationToken(org.acegisecurity.providers.UsernamePasswordAuthenticationToken) Principal(com.autentia.tnt.manager.security.Principal)

Example 9 with Principal

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;
}
Also used : Principal(com.autentia.tnt.manager.security.Principal) NotFoundException(org.acegisecurity.acls.NotFoundException)

Example 10 with Principal

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);
}
Also used : AuthenticationManager(com.autentia.tnt.manager.security.AuthenticationManager) Authentication(org.acegisecurity.Authentication) UsernamePasswordAuthenticationToken(org.acegisecurity.providers.UsernamePasswordAuthenticationToken) Principal(com.autentia.tnt.manager.security.Principal)

Aggregations

Principal (com.autentia.tnt.manager.security.Principal)13 UsernamePasswordAuthenticationToken (org.acegisecurity.providers.UsernamePasswordAuthenticationToken)7 Authentication (org.acegisecurity.Authentication)6 AccountEntry (com.autentia.tnt.businessobject.AccountEntry)2 AccountEntryType (com.autentia.tnt.businessobject.AccountEntryType)2 Activity (com.autentia.tnt.businessobject.Activity)2 AdminHoliday (com.autentia.tnt.businessobject.AdminHoliday)2 Bill (com.autentia.tnt.businessobject.Bill)2 Book (com.autentia.tnt.businessobject.Book)2 BulletinBoard (com.autentia.tnt.businessobject.BulletinBoard)2 BulletinBoardCategory (com.autentia.tnt.businessobject.BulletinBoardCategory)2 CompanyState (com.autentia.tnt.businessobject.CompanyState)2 Contact (com.autentia.tnt.businessobject.Contact)2 ContractType (com.autentia.tnt.businessobject.ContractType)2 Department (com.autentia.tnt.businessobject.Department)2 FinancialRatio (com.autentia.tnt.businessobject.FinancialRatio)2 Holiday (com.autentia.tnt.businessobject.Holiday)2 Idea (com.autentia.tnt.businessobject.Idea)2 Interaction (com.autentia.tnt.businessobject.Interaction)2 InteractionType (com.autentia.tnt.businessobject.InteractionType)2