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.util.PermissionUtils;
20 
21 import hunt.shiro.authz.permission.Permission;
22 import hunt.shiro.authz.permission.PermissionResolver;
23 
24 import hunt.collection;
25 import hunt.Exceptions;
26 import hunt.text.StringUtils;
27 import hunt.util.ArrayHelper;
28 
29 import std.array;
30 
31 /**
32  * Utility class to help with string-to-Permission object resolution.
33  *
34  */
35 class PermissionUtils {
36 
37     static Set!(Permission) resolveDelimitedPermissions(string s, PermissionResolver permissionResolver) {
38         Set!(string) permStrings = toPermissionStrings(s);
39         return resolvePermissions(permStrings, permissionResolver);
40     }
41 
42     static Set!(string) toPermissionStrings(string permissionsString) {
43         string[] tokens = StringUtils.split(permissionsString);
44         if (!tokens.empty()) {
45             return new LinkedHashSet!(string)(tokens);
46         }
47         return null;
48     }
49 
50     static Set!(Permission) resolvePermissions(Collection!(string) permissionStrings, 
51         PermissionResolver permissionResolver) {
52         Set!(Permission) permissions = new LinkedHashSet!(Permission)(permissionStrings.size());
53         foreach(string permissionString ; permissionStrings) {
54             permissions.add(permissionResolver.resolvePermission(permissionString));
55         }
56         return permissions;
57     }
58 }