View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.security;
35  
36  import java.util.Collection;
37  import java.util.Collections;
38  
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  
43  /**
44   * A group implementation. This bean is not connected to the underlying group storage.
45   */
46  public class MgnlGroup implements Group {
47      private static final Logger log = LoggerFactory.getLogger(MgnlGroup.class);
48  
49      private final String name;
50      private final Collection<String> groups;
51      private final Collection<String> roles;
52      private final String id;
53  
54      public MgnlGroup(String id, String groupName, Collection<String> subgroupNames, Collection<String> roleNames) {
55          this.id = id;
56          this.name = groupName;
57          this.groups = Collections.unmodifiableCollection(subgroupNames);
58          this.roles = Collections.unmodifiableCollection(roleNames);
59      }
60  
61      @Override
62      public String getName() {
63          return this.name;
64      }
65  
66      @Override
67      public void addRole(String roleName) throws UnsupportedOperationException, AccessDeniedException {
68          throw new UnsupportedOperationException("Use manager to modify this group");
69      }
70  
71      /**
72       * Add a subgroup to this group.
73       */
74      @Override
75      public void addGroup(String groupName) throws UnsupportedOperationException, AccessDeniedException {
76          throw new UnsupportedOperationException("Use manager to modify this group");
77      }
78  
79      @Override
80      public void removeRole(String roleName) throws UnsupportedOperationException, AccessDeniedException {
81          throw new UnsupportedOperationException("Use manager to modify this group");
82      }
83  
84      /**
85       * Remove a subgroup from this group.
86       */
87      @Override
88      public void removeGroup(String groupName) throws UnsupportedOperationException, AccessDeniedException {
89          throw new UnsupportedOperationException("Use manager to modify this group");
90      }
91  
92      @Override
93      public boolean hasRole(String roleName) throws UnsupportedOperationException, AccessDeniedException {
94          return this.roles.contains(roleName);
95      }
96  
97      /**
98       * FIXME: Yet another method that is questionable. Either we take all arbitrary properties of the group and load them in memory,
99       * wasting the time and memory on group instance creation or such props should be requested on demand from the manager.
100      * The fact that group interface exposes also setter for this method makes it even worse!
101      */
102     @Override
103     public String getProperty(String propertyName) {
104         throw new UnsupportedOperationException("Use manager to retrieve arbitrary group properties");
105     }
106 
107     @Override
108     public void setProperty(String propertyName, String value) {
109         throw new UnsupportedOperationException("Use manager to modify this group");
110     }
111 
112     /**
113      * Returns read only roles collection.
114      *
115      * @see info.magnolia.cms.security.Group#getRoles()
116      */
117     @Override
118     public Collection<String> getRoles() {
119         return Collections.unmodifiableCollection(this.roles);
120     }
121 
122     /**
123      * Returns read only groups collection.
124      *
125      * @see info.magnolia.cms.security.Group#getGroups()
126      */
127     @Override
128     public Collection<String> getGroups() {
129         return this.groups;
130     }
131 
132     @Override
133     public Collection<String> getAllGroups() {
134         throw new UnsupportedOperationException("Use manager to modify this group");
135     }
136 
137     @Override
138     public String getId() {
139         return id;
140     }
141 }