org.waterforpeople.mapping.app.web.rest.dto.UserAuthorizationPayload.java Source code

Java tutorial

Introduction

Here is the source code for org.waterforpeople.mapping.app.web.rest.dto.UserAuthorizationPayload.java

Source

/*
 *  Copyright (C) 2014 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW 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 Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package org.waterforpeople.mapping.app.web.rest.dto;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.waterforpeople.mapping.app.web.rest.security.AppRole;

import com.gallatinsystems.framework.gwt.dto.client.BaseDto;
import com.gallatinsystems.user.domain.UserAuthorization;

/**
 * Wrapper and DTO class to hide the internals of @{link com.gallatinsystems.user.UserAuthorization}
 * class and pass data to and from REST requests
 *
 * @author emmanuel
 */
public class UserAuthorizationPayload extends BaseDto {

    private static final long serialVersionUID = -2483907342483369954L;

    private UserAuthorization userAuthorization;

    public UserAuthorizationPayload(UserAuthorization userAuthorization) {
        this.userAuthorization = userAuthorization;
        if (userAuthorization.getKey() != null) {
            this.setKeyId(userAuthorization.getKey().getId());
        }
    }

    public UserAuthorizationPayload() {
        this.userAuthorization = new UserAuthorization();
    }

    public Long getUserId() {
        return userAuthorization.getUserId();
    }

    public void setUserId(Long userId) {
        this.userAuthorization.setUserId(userId);
    }

    public Long getRoleId() {
        return userAuthorization.getRoleId();
    }

    public void setRoleId(Long roleId) {
        this.userAuthorization.setRoleId(roleId);
    }

    public String getObjectPath() {
        return userAuthorization.getObjectPath();
    }

    public void setObjectPath(String objectPath) {
        if (isAcceptablePath(objectPath)) {
            this.userAuthorization.setObjectPath(objectPath);
        } else {
            throw new IllegalArgumentException("The specified path is not acceptable (" + objectPath + ")");
        }
    }

    private boolean isAcceptablePath(String objectPath) {
        if (StringUtils.isNotBlank(objectPath)) {
            if ("/".equals(objectPath)) {
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                return authentication.getAuthorities().contains(AppRole.SUPER_ADMIN);
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    @JsonIgnore
    public UserAuthorization getUserAuthorisation() {
        return userAuthorization;
    }
}