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.permission.DomainPermission;
20 
21 import hunt.shiro.authz.permission.WildcardPermission;
22 // import hunt.shiro.util.StringUtils;
23 
24 import hunt.collection.Set;
25 
26 /**
27  * Provides a base Permission class from which type-safe/domain-specific subclasses may extend.  Can be used
28  * as a base class for JPA/Hibernate persisted permissions that wish to store the parts of the permission string
29  * in separate columns (e.g. 'domain', 'actions' and 'targets' columns), which can be used in querying
30  * strategies.
31  *
32  */
33 // class DomainPermission : WildcardPermission {
34 
35 //     private string domain;
36 //     private Set!(string) actions;
37 //     private Set!(string) targets;
38 
39 
40 //     /**
41 //      * Creates a domain permission with *all* actions for *all* targets;
42 //      */
43 //      this() {
44 //         this.domain = getDomain(getClass());
45 //         setParts(getDomain(getClass()));
46 //     }
47 
48 //      this(string actions) {
49 //         domain = getDomain(getClass());
50 //         this.actions = StringUtils.splitToSet(actions, SUBPART_DIVIDER_TOKEN);
51 //         encodeParts(domain, actions, null);
52 //     }
53 
54 //      this(string actions, string targets) {
55 //         this.domain = getDomain(getClass());
56 //         this.actions = StringUtils.splitToSet(actions, SUBPART_DIVIDER_TOKEN);
57 //         this.targets = StringUtils.splitToSet(targets, SUBPART_DIVIDER_TOKEN);
58 //         encodeParts(this.domain, actions, targets);
59 //     }
60 
61 //     protected this(Set!(string) actions, Set!(string) targets) {
62 //         this.domain = getDomain(getClass());
63 //         setParts(domain, actions, targets);
64 //     }
65 
66 //     private void encodeParts(string domain, string actions, string targets) {
67 //         if (!StringUtils.hasText(domain)) {
68 //             throw new IllegalArgumentException("domain argument cannot be null or empty.");
69 //         }
70 //         StringBuilder sb = new StringBuilder(domain);
71 
72 //         if (!StringUtils.hasText(actions)) {
73 //             if (StringUtils.hasText(targets)) {
74 //                 sb.append(PART_DIVIDER_TOKEN).append(WILDCARD_TOKEN);
75 //             }
76 //         } else {
77 //             sb.append(PART_DIVIDER_TOKEN).append(actions);
78 //         }
79 //         if (StringUtils.hasText(targets)) {
80 //             sb.append(PART_DIVIDER_TOKEN).append(targets);
81 //         }
82 //         setParts(sb.toString());
83 //     }
84 
85 //     protected void setParts(string domain, Set!(string) actions, Set!(string) targets) {
86 //         string actionsString = StringUtils.toDelimitedString(actions, SUBPART_DIVIDER_TOKEN);
87 //         string targetsString = StringUtils.toDelimitedString(targets, SUBPART_DIVIDER_TOKEN);
88 //         encodeParts(domain, actionsString, targetsString);
89 //         this.domain = domain;
90 //         this.actions = actions;
91 //         this.targets = targets;
92 //     }
93 
94 //     protected string getDomain(TypeInfo_Class clazz) {
95 //         string domain = clazz.getSimpleName().toLowerCase();
96 //         //strip any trailing 'permission' text from the name (as all subclasses should have been named):
97 //         int index = domain.lastIndexOf("permission");
98 //         if (index != -1) {
99 //             domain = domain.substring(0, index);
100 //         }
101 //         return domain;
102 //     }
103 
104 //      string getDomain() {
105 //         return domain;
106 //     }
107 
108 //     protected void setDomain(string domain) {
109 //         if (this.domain !is null && this.domain== domain) {
110 //             return;
111 //         }
112 //         this.domain = domain;
113 //         setParts(domain, actions, targets);
114 //     }
115 
116 //      Set!(string) getActions() {
117 //         return actions;
118 //     }
119 
120 //     protected void setActions(Set!(string) actions) {
121 //         if (this.actions !is null && this.actions== actions) {
122 //             return;
123 //         }
124 //         this.actions = actions;
125 //         setParts(domain, actions, targets);
126 //     }
127 
128 //      Set!(string) getTargets() {
129 //         return targets;
130 //     }
131 
132 //     protected void setTargets(Set!(string) targets) {
133 //         if (this.targets !is null && this.targets== targets) {
134 //             return;
135 //         }
136 //         this.targets = targets;
137 //         setParts(domain, actions, targets);
138 //     }
139 // }