rights.UserRightsUtil.java Source code

Java tutorial

Introduction

Here is the source code for rights.UserRightsUtil.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rights;

import java.util.Collection;
import java.util.HashMap;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 *
 * @author Rice Pavel
 */
public class UserRightsUtil {

    /*public HashMap<String, Boolean> userCommonRights = getCommonRights();
        
    private HashMap<String, Boolean> getCommonRights() {
    HashMap<String, Boolean> commonRights = new HashMap();
    Collection<? extends GrantedAuthority> grantes = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
    for (GrantedAuthority grant : grantes) {
        String right = grant.getAuthority();
        commonRights.put(right, true);
    }
    return commonRights;
    }*/

    public static boolean isRight(Rights right) {
        return isRight(right.toString());
    }

    public static boolean isRight(String right) {
        Collection<? extends GrantedAuthority> grantes = SecurityContextHolder.getContext().getAuthentication()
                .getAuthorities();
        for (GrantedAuthority grant : grantes) {
            if (grant.getAuthority().equals(right)) {
                return true;
            }
        }
        return false;
    }

}