Project

General

Profile

Download (7.78 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.model.permission;
10

    
11
import java.util.HashSet;
12
import java.util.Set;
13
import java.util.UUID;
14

    
15
import javax.persistence.Column;
16
import javax.persistence.Entity;
17
import javax.persistence.FetchType;
18
import javax.persistence.ManyToMany;
19
import javax.persistence.Table;
20
import javax.validation.constraints.NotNull;
21
import javax.validation.constraints.Pattern;
22
import javax.xml.bind.annotation.XmlAccessType;
23
import javax.xml.bind.annotation.XmlAccessorType;
24
import javax.xml.bind.annotation.XmlElement;
25
import javax.xml.bind.annotation.XmlElementWrapper;
26
import javax.xml.bind.annotation.XmlIDREF;
27
import javax.xml.bind.annotation.XmlRootElement;
28
import javax.xml.bind.annotation.XmlSchemaType;
29
import javax.xml.bind.annotation.XmlType;
30

    
31
import org.apache.commons.lang3.StringUtils;
32
import org.apache.logging.log4j.LogManager;
33
import org.apache.logging.log4j.Logger;
34
import org.hibernate.annotations.Cascade;
35
import org.hibernate.annotations.CascadeType;
36
import org.hibernate.search.annotations.Field;
37
import org.springframework.security.core.GrantedAuthority;
38

    
39
import eu.etaxonomy.cdm.model.common.CdmBase;
40

    
41
@XmlAccessorType(XmlAccessType.FIELD)
42
@XmlType(name = "Group", propOrder = {
43
    "name",
44
    "members",
45
    "grantedAuthorities",
46
    "authorities",
47
})
48
@XmlRootElement(name = "Group")
49
@Entity
50
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
51
//@Indexed(index = "eu.etaxonomy.cdm.model.common.Group")
52
@Table(name = "PermissionGroup")
53
public class Group extends CdmBase {
54

    
55
    private static final long serialVersionUID = 7216686200093054648L;
56
    private static final Logger logger = LogManager.getLogger(Group.class);
57

    
58
    public final static UUID GROUP_EDITOR_UUID = UUID.fromString("22e5e8af-b99c-4884-a92f-71978efd3770");
59
    public final static UUID GROUP_EDITOR_EXTENDED_CREATE_UUID = UUID.fromString("89a7f47f-6f2b-45ac-88d4-a99a4cf29f07");
60
    public final static UUID GROUP_PROJECT_MANAGER_UUID = UUID.fromString("645191ae-32a4-4d4e-9b86-c90e0d41944a");
61
    public final static UUID GROUP_PUBLISH_UUID = UUID.fromString("c1f20ad8-1782-40a7-b06b-ce4773acb5ea");
62
    public final static UUID GROUP_ADMIN_UUID = UUID.fromString("1739df71-bf73-4dc6-8320-aaaf72cb555f");
63
    public static final UUID GROUP_EDITOR_REFERENCE_UUID = UUID.fromString("c3efd156-3a0a-4a5c-a99c-c6262fd734bd");
64
    public static final UUID GROUP_ALLOW_ALL_TAXA_UUID = UUID.fromString("08eadacd-e060-4e96-a40d-75cd495c304a");
65
    public final static UUID GROUP_USER_MANAGER_UUID = UUID.fromString("7dee66be-121a-4d7f-a91d-b37390a4d2cc");
66

    
67
    public final static String GROUP_EDITOR_NAME = "Editor";
68
    /**
69
     * This group will in future replace the group Editor, see issue #7150
70
     */
71
    public final static String GROUP_EDITOR_EXTENDED_CREATE_NAME = "Editor_Extended_Create";
72
    public final static String GROUP_PROJECT_MANAGER_NAME = "Project_Manager";
73
    public final static String GROUP_ADMIN_NAME = "Admin";
74
    public static final String GROUP_EDITOR_REFERENCE_NAME = "Editor_Reference";
75
    public static final String GROUP_ALLOW_ALL_TAXA_NAME = "Allow_for_all_taxa";
76
    public static final String GROUP_PUBLISH_NAME = "Publish";
77
    public final static String GROUP_USER_MANAGER_NAME = "User_Manager";
78

    
79
    //Phycobank groups  //for now handled here, might be handled elsewhere later
80
    public final static String GROUP_SUBMITTER = "Submitter";
81

    
82
//**************** FIELDS ******************************/
83

    
84
    @XmlElement(name = "Name")
85
    @Column(unique = true)
86
    @Field
87
    @NotNull
88
    @Pattern(regexp=User.USERNAME_REGEX)
89
    private String name;
90

    
91
    @XmlElementWrapper(name = "Members")
92
    @XmlElement(name = "Member")
93
    @XmlIDREF
94
    @XmlSchemaType(name = "IDREF")
95
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
96
    private Set<User> members = new HashSet<>();
97

    
98
    @XmlElementWrapper(name = "GrantedAuthorities")
99
    @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
100
    @XmlIDREF
101
    @XmlSchemaType(name = "IDREF")
102
    @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
103
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
104
    private Set <GrantedAuthority> grantedAuthorities = new HashSet<>();
105

    
106
    @XmlElementWrapper(name = "Authorities")
107
    @XmlElement(name = "Authority", type = AuthorityBase.class)
108
    @XmlIDREF
109
    @XmlSchemaType(name = "IDREF")
110
    @ManyToMany(fetch = FetchType.LAZY, targetEntity = AuthorityBase.class)
111
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
112
    private Set <AuthorityBase> authorities = new HashSet<>();
113

    
114

    
115
  //*********************** FACTORY *********************/
116

    
117
    public static Group NewInstance(){
118
        return new Group();
119
    }
120

    
121
    public static Group NewInstance(String name){
122
        Group group = Group.NewInstance();
123
        group.setName(name);
124
        return group;
125
    }
126

    
127
// ********************* CONSTRUCTOR ************************/
128

    
129
    //for internal use only, *packet* private required by bytebuddy
130
    Group(){}
131

    
132
// *************** GETTER / SETTER ***********************************/
133

    
134
    public Set<GrantedAuthority> getGrantedAuthorities() {
135
        return grantedAuthorities;
136
    }
137
    public boolean addGrantedAuthority(GrantedAuthority grantedAuthority){
138
        return grantedAuthorities.add(grantedAuthority);
139
    }
140
    public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority){
141
        return grantedAuthorities.remove(grantedAuthority);
142
    }
143

    
144
    public Set<AuthorityBase> getAuthorities() {
145
        return authorities;
146
    }
147
    public boolean addAuthority(AuthorityBase authority){
148
        return authorities.add(authority);
149
    }
150
    public boolean removeAuthority(AuthorityBase authority){
151
        return authorities.remove(authority);
152
    }
153

    
154
    public String getName() {return name;}
155
    public void setName(String name) {this.name = name;}
156

    
157

    
158
    public Set<User> getMembers() {
159
        return members;
160
    }
161
    public boolean addMember(User user) {
162
        user.getGroups().add(this);
163
        return this.members.add(user);
164
    }
165
    public boolean removeMember(User user) {
166
        if(members.contains(user)) {
167
            user.getGroups().remove(this);
168
            return this.members.remove(user);
169
        } else {
170
            return false;
171
        }
172
    }
173

    
174
//*********************** CLONE ********************************************************/
175

    
176
    /**
177
     * Clones <i>this</i> Group. This is a shortcut that enables to create
178
     * a new instance that differs only slightly from <i>this</i> group by
179
     * modifying only some of the attributes.
180
     *
181
     * @see eu.etaxonomy.cdm.model.term.TermBase#clone()
182
     * @see java.lang.Object#clone()
183
     */
184
    @Override
185
    public Group clone() {
186
        try{
187
            Group result = (Group)super.clone();
188
            result.grantedAuthorities = new HashSet<>();
189
            for (GrantedAuthority grantedauthority: this.grantedAuthorities){
190
                result.addGrantedAuthority(grantedauthority);
191
            }
192

    
193
            result.members = new HashSet<>();
194
            for (User member: this.members){
195
                result.addMember(member);
196
            }
197

    
198
            //no changes to name
199
            return result;
200
        } catch (CloneNotSupportedException e) {
201
            logger.warn("Object does not implement cloneable");
202
            e.printStackTrace();
203
            return null;
204
        }
205
    }
206

    
207
//************************************** toString ***************************************
208

    
209
    @Override
210
    public String toString() {
211
        if (StringUtils.isNotBlank(name)){
212
            return name;
213
        }else{
214
            return super.toString();
215
        }
216
    }
217
}
(5-5/9)