/*
 *  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;
    }
}

@@ -18,16 +18,21 @@
 (C) 2014
+-2015
  Stichti
@@ -757,16 +757,46 @@
 t.dto;%0A%0A
+import java.util.Collection;%0A%0A
 import o
@@ -921,30 +921,32 @@
 ty.core.
-Authentication
+GrantedAuthority
 ;%0Aimport
@@ -2865,37 +2865,58 @@
     
-Authentication authentication
+Collection%3C? extends GrantedAuthority%3E authorities
  = S
@@ -2939,21 +2939,8 @@
 lder
-.getContext()
 %0A   
@@ -2960,16 +2960,29 @@
         
+.getContext()
 .getAuth
@@ -2989,25 +2989,43 @@
 entication()
-;
+.getAuthorities();%0A
 %0A           
@@ -3044,23 +3044,74 @@
 auth
-entication.getA
+orities.contains(AppRole.SUPER_ADMIN)%0A                        %7C%7C a
 utho
@@ -3116,18 +3116,16 @@
 horities
-()
 .contain
@@ -3138,22 +3138,16 @@
 ole.
-SUPER_
 ADMIN);%0A
     
@@ -3142,16 +3142,17 @@
 ADMIN);%0A
+%0A
         

Allow admins to assign permissions to the root path