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 module hunt.shiro.authz.SimpleRole; 20 21 import hunt.shiro.authz.permission.Permission; 22 23 import hunt.util.Common; 24 import hunt.collection; 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 */ 31 class SimpleRole { 32 33 protected string name = null; 34 protected Set!(Permission) permissions; 35 36 this() { 37 } 38 39 this(string name) { 40 setName(name); 41 } 42 43 this(string name, Set!(Permission) permissions) { 44 setName(name); 45 setPermissions(permissions); 46 } 47 48 string getName() @trusted nothrow { 49 return name; 50 } 51 52 void setName(string name) @trusted nothrow { 53 this.name = name; 54 } 55 56 Set!(Permission) getPermissions() { 57 return permissions; 58 } 59 60 void setPermissions(Set!(Permission) permissions) { 61 this.permissions = permissions; 62 } 63 64 void add(Permission permission) { 65 Set!(Permission) permissions = getPermissions(); 66 if (permissions is null) { 67 permissions = new LinkedHashSet!(Permission)(); 68 setPermissions(permissions); 69 } 70 permissions.add(permission); 71 } 72 73 void addAll(Collection!(Permission) perms) { 74 if (perms !is null && !perms.isEmpty()) { 75 Set!(Permission) permissions = getPermissions(); 76 if (permissions is null) { 77 permissions = new LinkedHashSet!(Permission)(perms.size()); 78 setPermissions(permissions); 79 } 80 permissions.addAll(perms); 81 } 82 } 83 84 bool isPermitted(Permission p) { 85 Collection!(Permission) perms = getPermissions(); 86 if (perms !is null && !perms.isEmpty()) { 87 foreach(Permission perm ; perms) { 88 if (perm.implies(p)) { 89 return true; 90 } 91 } 92 } 93 return false; 94 } 95 96 override size_t toHash() @trusted nothrow { 97 return (getName() !is null ? getName().hashOf() : 0); 98 } 99 100 override bool opEquals(Object o) { 101 if (o == this) { 102 return true; 103 } 104 auto oCast = cast(SimpleRole)o; 105 if (oCast !is null) { 106 SimpleRole sr = oCast; 107 //only check name, since role names should be unique across an entire application: 108 return (getName() !is null ? getName()== sr.getName() : sr.getName() is null); 109 } 110 return false; 111 } 112 113 override string toString() { 114 return getName(); 115 } 116 }