(no commit message)
[utils] / security / usermgt / 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 import javax.persistence.Column;
19 import javax.persistence.DiscriminatorValue;
20 import javax.persistence.Entity;
21 import javax.persistence.Transient;
22
23 import org.wamblee.security.authentication.UserAdministration;
24
25 /**
26  * Checks if a user against a specific group.
27  * 
28  * @author Erik Brakkee
29  */
30 @Entity
31 @DiscriminatorValue("GROUP")
32 public class GroupUserCondition extends AbstractUserCondition {
33     /**
34      * Group the user must be in.
35      */
36     @Column(name = "GRP")
37     private String group;
38
39     @Transient
40     private UserAdministration admin;
41
42     /**
43      * Constructs the condition.
44      * 
45      * @param aGroup
46      *            Group the user must be in.
47      */
48     public GroupUserCondition(String aGroup) {
49         group = aGroup;
50     }
51
52     /**
53      * For OR mapping.
54      * 
55      */
56     protected GroupUserCondition() {
57         group = null;
58     }
59
60     @Override
61     public void setUserAdmin(UserAdministration aAdmin) {
62         admin = aAdmin;
63     }
64
65     @Override
66     public boolean matches(String aUser) {
67         return admin.isInGroup(aUser, group);
68     }
69
70     /**
71      * 
72      * @return Returns the group.
73      */
74     protected String getGroup() {
75         return group;
76     }
77
78     /**
79      * 
80      * @param aGroup
81      *            The group to set.
82      */
83     protected void setGroup(String aGroup) {
84         group = aGroup;
85     }
86
87     /*
88      * (non-Javadoc)
89      * 
90      * @see java.lang.Object#toString()
91      */
92     @Override
93     public String toString() {
94         return "GroupUserCondition(group=" + group + ")";
95     }
96 }