001/** 002 * Copyright 2014 Tampere University of Technology, Pori Department 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package core.tut.pori.users; 017 018import org.springframework.security.core.GrantedAuthority; 019import org.springframework.security.core.authority.SimpleGrantedAuthority; 020 021/** 022 * utility class for creating user authorities, and converting role string to Authority objects. 023 * 024 */ 025public final class UserAuthority { 026 /** admin role string */ 027 public static final String ROLE_ADMIN = "ROLE_ADMIN"; 028 /** back-end role string */ 029 public static final String ROLE_BACKEND = "ROLE_BACKEND"; 030 /** user role string */ 031 public static final String ROLE_USER = "ROLE_USER"; 032 /** authority admin */ 033 public static final GrantedAuthority AUTHORITY_ROLE_ADMIN = new SimpleGrantedAuthority(ROLE_ADMIN); 034 /** authority back-end */ 035 public static final GrantedAuthority AUTHORITY_ROLE_BACKEND = new SimpleGrantedAuthority(ROLE_BACKEND); 036 /** authority user */ 037 public static final GrantedAuthority AUTHORITY_ROLE_USER = new SimpleGrantedAuthority(ROLE_USER); 038 039 /** 040 * 041 * @param roleString 042 * @return the given string converted to an authority 043 * @throws IllegalArgumentException on bad string 044 */ 045 public static GrantedAuthority getGrantedAuthority(String roleString) throws IllegalArgumentException { 046 if(roleString != null){ 047 switch (roleString) { 048 case ROLE_USER: 049 return AUTHORITY_ROLE_USER; 050 case ROLE_ADMIN: 051 return AUTHORITY_ROLE_ADMIN; 052 case ROLE_BACKEND: 053 return AUTHORITY_ROLE_BACKEND; 054 default: 055 break; 056 } 057 } 058 throw new IllegalArgumentException("Bad "+GrantedAuthority.class.toString()+" : "+roleString); 059 } 060}