br.com.criativasoft.opendevice.restapi.auth.AbstractAuthorizingRealm.java Source code

Java tutorial

Introduction

Here is the source code for br.com.criativasoft.opendevice.restapi.auth.AbstractAuthorizingRealm.java

Source

/*
 * *****************************************************************************
 * Copyright (c) 2013-2014 CriativaSoft (www.criativasoft.com.br)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *  Ricardo JL Rufino - Initial API and Implementation
 * *****************************************************************************
 */

package br.com.criativasoft.opendevice.restapi.auth;

import br.com.criativasoft.opendevice.restapi.model.AccountType;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Ricardo JL Rufino
 * @date 17/10/16
 */
public abstract class AbstractAuthorizingRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        if (principals.isEmpty())
            throw new AuthorizationException("Empty principals list!");

        AccountPrincipal principal = (AccountPrincipal) principals.getPrimaryPrincipal();

        Set<String> roles = new HashSet(Arrays.asList(principal.getType().name()));

        if (principal.getType() == AccountType.CLOUD_MANAGER)
            roles.add(AccountType.ROLES.ACCOUNT_MANAGER);

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
        return info;
    }
}