(no commit message)
[utils] / security / impl / src / main / java / org / wamblee / security / authorization / GroupUserCondition.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.security.authorization;
17
18
19 import javax.persistence.Column;
20 import javax.persistence.DiscriminatorValue;
21 import javax.persistence.Entity;
22 import javax.persistence.Transient;
23
24 import org.wamblee.security.authentication.UserAdministration;
25
26
27 /**
28  * Checks if a user against a specific group.
29  * 
30  * @author Erik Brakkee
31  */
32 @Entity
33 @DiscriminatorValue("GROUP")
34 public class GroupUserCondition extends AbstractUserCondition {
35     /**
36      * Group the user must be in.
37      */
38     @Column(name = "GRP")
39     private String group;
40     
41     @Transient
42     private UserAdministration admin; 
43
44     /**
45      * Constructs the condition.
46      * 
47      * @param aGroup
48      *            Group the user must be in.
49      */
50     public GroupUserCondition(String aGroup) {
51         group = aGroup;
52     }
53
54     /**
55      * For OR mapping.
56      * 
57      */
58     protected GroupUserCondition() {
59         group = null;
60     }
61     
62     @Override
63     public void setUserAdmin(UserAdministration aAdmin) {
64         admin = aAdmin;   
65     }
66
67     @Override
68     public boolean matches(String aUser) {
69         return admin.isInGroup(aUser, group);
70     }
71
72     /**
73      * 
74      * @return Returns the group.
75      */
76     protected String getGroup() {
77         return group;
78     }
79
80     /**
81      * 
82      * @param aGroup
83      *            The group to set.
84      */
85     protected void setGroup(String aGroup) {
86         group = aGroup;
87     }
88
89     /*
90      * (non-Javadoc)
91      * 
92      * @see java.lang.Object#toString()
93      */
94     @Override
95     public String toString() {
96         return "GroupUserCondition(group=" + group + ")";
97     }
98 }