com.seajas.search.utilities.spring.security.model.ExtendedCrowdUserDetails.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.utilities.spring.security.model.ExtendedCrowdUserDetails.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.utilities.spring.security.model;

import com.atlassian.crowd.integration.soap.SOAPPrincipal;
import com.atlassian.crowd.integration.springsecurity.user.CrowdUserDetails;
import com.atlassian.crowd.model.user.UserWithAttributes;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;

/**
 * Crowd user details, including additional properties.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@SuppressWarnings("deprecation")
public class ExtendedCrowdUserDetails extends CrowdUserDetails implements ExtendedUserDetails {
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Username constants.
     */
    private static final String USERNAME_ADMIN = "admin";

    /**
     * Role constants.
     */
    private static final String ROLE_MAINTAINER = "ROLE_MAINTAINER";
    private static final String ROLE_SUPERVISOR = "ROLE_SUPERVISOR";

    /**
     * User object.
     */
    private User user;

    /**
     * Default constructor.
     * 
     * @param principal
     * @param authorities
     */
    public ExtendedCrowdUserDetails(final SOAPPrincipal principal, final GrantedAuthority[] authorities) {
        super(principal, authorities);
    }

    /**
     * Default constructor which uses a CrowdUserDetails as a reference to add in the User.
     *
     * @param details
     * @param user
     */
    public ExtendedCrowdUserDetails(final CrowdUserDetails details, final User user) {
        super(extractPrincipal(details), getAuthoritiesFromUser(user));

        this.user = user;
    }

    /**
     * Illegally extract the required SOAPPrincipal.
     *
     * @param details
     * @return SOAPPrincipal
     */
    private static SOAPPrincipal extractPrincipal(final CrowdUserDetails details) {
        UserWithAttributes attributes = details.getRemotePrincipal();

        try {
            Field principalField = attributes.getClass().getDeclaredField("principal");

            principalField.setAccessible(true);

            return (SOAPPrincipal) principalField.get(attributes);
        } catch (NoSuchFieldException e) {
            throw new IllegalArgumentException("Didn't find user details which holds a SOAPPrincipal", e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException("Didn't find user details which holds a SOAPPrincipal", e);
        }
    }

    /**
     * Return the authorities.
     * 
     * @param user
     * @return GrantedAuthority[]
     */
    public static Collection<GrantedAuthority> getAuthoritiesFromUser(final User user) {
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

        authorities.add(new GrantedAuthorityImpl(ROLE_MAINTAINER));

        if (user.getUsername().equals(USERNAME_ADMIN))
            authorities.add(new GrantedAuthorityImpl(ROLE_SUPERVISOR));

        return authorities;
    }

    /**
     * Return the associated user.
     *
     * @return User
     */
    @Override
    public User getUser() {
        return user;
    }
}