View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 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 org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import java.util.Collection;
40  import java.util.Collections;
41  
42  
43  /**
44   * A group implementation. This bean is not connected to the underlying group storage.
45   * @author Sameer Charles $Id$
46   */
47  public class MgnlGroup implements Group {
48      private static final Logger log = LoggerFactory.getLogger(MgnlGroup.class);
49  
50      private final String name;
51      private final Collection<String> groups;
52      private final Collection<String> roles;
53      private final String id;
54  
55      public MgnlGroup(String id, String groupName, Collection<String> subgroupNames, Collection<String> roleNames) {
56          this.id = id;
57          this.name = groupName;
58          this.groups = Collections.unmodifiableCollection(subgroupNames);
59          this.roles = Collections.unmodifiableCollection(roleNames);
60      }
61  
62      @Override
63      public String getName() {
64          return this.name;
65      }
66  
67      @Override
68      public void addRole(String roleName) throws UnsupportedOperationException, AccessDeniedException {
69          throw new UnsupportedOperationException("Use manager to modify this group");
70      }
71  
72      /**
73       * Add a subgroup to this group.
74       */
75      @Override
76      public void addGroup(String groupName) throws UnsupportedOperationException, AccessDeniedException {
77          throw new UnsupportedOperationException("Use manager to modify this group");
78      }
79  
80      @Override
81      public void removeRole(String roleName) throws UnsupportedOperationException, AccessDeniedException {
82          throw new UnsupportedOperationException("Use manager to modify this group");
83      }
84  
85      /**
86       * Remove a subgroup from this group.
87       */
88      @Override
89      public void removeGroup(String groupName) throws UnsupportedOperationException, AccessDeniedException {
90          throw new UnsupportedOperationException("Use manager to modify this group");
91      }
92  
93      @Override
94      public boolean hasRole(String roleName) throws UnsupportedOperationException, AccessDeniedException {
95          return this.roles.contains(roleName);
96      }
97  
98      /**
99       * FIXME: Yet another method that is questionable. Either we take all arbitrary properties of the group and load them in memory,
100      * wasting the time and memory on group instance creation or such props should be requested on demand from the manager.
101      * The fact that group interface exposes also setter for this method makes it even worse!
102      */
103     @Override
104     public String getProperty(String propertyName) {
105         throw new UnsupportedOperationException("Use manager to retrieve arbitrary group properties");
106     }
107 
108     @Override
109     public void setProperty(String propertyName, String value) {
110         throw new UnsupportedOperationException("Use manager to modify this group");
111     }
112 
113     /**
114      * Returns read only roles collection.
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      * @see info.magnolia.cms.security.Group#getGroups()
125      */
126     @Override
127     public Collection<String> getGroups() {
128         return this.groups;
129     }
130 
131     @Override
132     public Collection<String> getAllGroups() {
133         throw new UnsupportedOperationException("Use manager to modify this group");
134     }
135 
136     @Override
137     public String getId() {
138         return id;
139     }
140 }