Coverage Report - org.apache.shiro.authz.SimpleRole
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleRole
22%
10/44
0%
0/26
2.154
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.shiro.authz;
 20  
 
 21  
 import java.io.Serializable;
 22  
 import java.util.Collection;
 23  
 import java.util.LinkedHashSet;
 24  
 import java.util.Set;
 25  
 
 26  
 /**
 27  
  * A simple representation of a security role that has a name and a collection of permissions.  This object can be
 28  
  * used internally by Realms to maintain authorization state.
 29  
  *
 30  
  * @since 0.2
 31  
  */
 32  
 public class SimpleRole implements Serializable {
 33  
 
 34  17
     protected String name = null;
 35  
     protected Set<Permission> permissions;
 36  
 
 37  0
     public SimpleRole() {
 38  0
     }
 39  
 
 40  17
     public SimpleRole(String name) {
 41  17
         setName(name);
 42  17
     }
 43  
 
 44  0
     public SimpleRole(String name, Set<Permission> permissions) {
 45  0
         setName(name);
 46  0
         setPermissions(permissions);
 47  0
     }
 48  
 
 49  
     public String getName() {
 50  17
         return name;
 51  
     }
 52  
 
 53  
     public void setName(String name) {
 54  17
         this.name = name;
 55  17
     }
 56  
 
 57  
     public Set<Permission> getPermissions() {
 58  24
         return permissions;
 59  
     }
 60  
 
 61  
     public void setPermissions(Set<Permission> permissions) {
 62  17
         this.permissions = permissions;
 63  17
     }
 64  
 
 65  
     public void add(Permission permission) {
 66  0
         Set<Permission> permissions = getPermissions();
 67  0
         if (permissions == null) {
 68  0
             permissions = new LinkedHashSet<Permission>();
 69  0
             setPermissions(permissions);
 70  
         }
 71  0
         permissions.add(permission);
 72  0
     }
 73  
 
 74  
     public void addAll(Collection<Permission> perms) {
 75  0
         if (perms != null && !perms.isEmpty()) {
 76  0
             Set<Permission> permissions = getPermissions();
 77  0
             if (permissions == null) {
 78  0
                 permissions = new LinkedHashSet<Permission>(perms.size());
 79  0
                 setPermissions(permissions);
 80  
             }
 81  0
             permissions.addAll(perms);
 82  
         }
 83  0
     }
 84  
 
 85  
     public boolean isPermitted(Permission p) {
 86  0
         Collection<Permission> perms = getPermissions();
 87  0
         if (perms != null && !perms.isEmpty()) {
 88  0
             for (Permission perm : perms) {
 89  0
                 if (perm.implies(p)) {
 90  0
                     return true;
 91  
                 }
 92  0
             }
 93  
         }
 94  0
         return false;
 95  
     }
 96  
 
 97  
     public int hashCode() {
 98  0
         return (getName() != null ? getName().hashCode() : 0);
 99  
     }
 100  
 
 101  
     public boolean equals(Object o) {
 102  0
         if (o == this) {
 103  0
             return true;
 104  
         }
 105  0
         if (o instanceof SimpleRole) {
 106  0
             SimpleRole sr = (SimpleRole) o;
 107  
             //only check name, since role names should be unique across an entire application:
 108  0
             return (getName() != null ? getName().equals(sr.getName()) : sr.getName() == null);
 109  
         }
 110  0
         return false;
 111  
     }
 112  
 
 113  
     public String toString() {
 114  0
         return getName();
 115  
     }
 116  
 }