001package org.apache.archiva.redback.rest.api.model;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import javax.xml.bind.annotation.XmlRootElement;
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.List;
025
026/**
027 * @author Olivier Lamy
028 * @since 1.4
029 */
030@XmlRootElement( name = "role" )
031public class Role
032    implements Serializable
033{
034    /**
035     * Field name
036     */
037    private String name;
038
039    /**
040     * Field description
041     */
042    private String description;
043
044    /**
045     * Field assignable
046     */
047    private boolean assignable = false;
048
049    /**
050     * Field childRoleNames
051     */
052    private List<String> childRoleNames = new ArrayList<String>(0);
053
054    /**
055     * Field permissions
056     */
057    private List<Permission> permissions = new ArrayList<Permission>(0);
058
059    /**
060     * some services doesn't populate this field getAllRoles in RoleManagementService
061     */
062    private List<String> parentRoleNames = new ArrayList<String>(0);
063
064    /**
065     * user with a parent role
066     * some services doesn't populate this field getAllRoles in RoleManagementService
067     */
068    private List<User> parentsRolesUsers = new ArrayList<User>(0);
069
070    /**
071     * user with this role
072     * some services doesn't populate this field getAllRoles in RoleManagementService
073     */
074    private List<User> users = new ArrayList<User>(0);
075
076    /**
077     * users without this role or parent role
078     * some services doesn't populate this field getAllRoles in RoleManagementService
079     */
080    private List<User> otherUsers = new ArrayList<User>(0);
081
082    /**
083     * users to remove assignement to this role
084     */
085    private List<User> removedUsers = new ArrayList<User>(0);
086
087    /**
088     * Field permanent
089     */
090    private boolean permanent = false;
091
092    public Role()
093    {
094        // no op
095    }
096
097    public Role( String name )
098    {
099        this.name = name;
100    }
101
102    public Role( org.apache.archiva.redback.rbac.Role role )
103    {
104        this.name = role.getName();
105        this.description = role.getDescription();
106        this.assignable = role.isAssignable();
107        this.childRoleNames = role.getChildRoleNames() == null
108            ? new ArrayList<String>( 0 )
109            : new ArrayList<String>( role.getChildRoleNames() );
110
111        if ( role.getPermissions() == null )
112        {
113            this.permissions = new ArrayList<Permission>( 0 );
114        }
115        else
116        {
117            for ( org.apache.archiva.redback.rbac.Permission p : role.getPermissions() )
118            {
119                this.permissions.add( new Permission( p ) );
120            }
121        }
122    }
123
124    public String getName()
125    {
126        return name;
127    }
128
129    public void setName( String name )
130    {
131        this.name = name;
132    }
133
134    public String getDescription()
135    {
136        return description;
137    }
138
139    public void setDescription( String description )
140    {
141        this.description = description;
142    }
143
144    public boolean isAssignable()
145    {
146        return assignable;
147    }
148
149    public void setAssignable( boolean assignable )
150    {
151        this.assignable = assignable;
152    }
153
154    public List<String> getChildRoleNames()
155    {
156        return childRoleNames;
157    }
158
159    public void setChildRoleNames( List<String> childRoleNames )
160    {
161        this.childRoleNames = childRoleNames;
162    }
163
164    public List<Permission> getPermissions()
165    {
166        return permissions;
167    }
168
169    public void setPermissions( List<Permission> permissions )
170    {
171        this.permissions = permissions;
172    }
173
174    public boolean isPermanent()
175    {
176        return permanent;
177    }
178
179    public void setPermanent( boolean permanent )
180    {
181        this.permanent = permanent;
182    }
183
184    public List<String> getParentRoleNames()
185    {
186        return parentRoleNames;
187    }
188
189    public void setParentRoleNames( List<String> parentRoleNames )
190    {
191        this.parentRoleNames = parentRoleNames;
192    }
193
194    public List<User> getParentsRolesUsers()
195    {
196        return parentsRolesUsers;
197    }
198
199    public void setParentsRolesUsers( List<User> parentsRolesUsers )
200    {
201        this.parentsRolesUsers = parentsRolesUsers;
202    }
203
204    public List<User> getUsers()
205    {
206        return users;
207    }
208
209    public void setUsers( List<User> users )
210    {
211        this.users = users;
212    }
213
214    public List<User> getOtherUsers()
215    {
216        return otherUsers;
217    }
218
219    public void setOtherUsers( List<User> otherUsers )
220    {
221        this.otherUsers = otherUsers;
222    }
223
224    public List<User> getRemovedUsers()
225    {
226        return removedUsers;
227    }
228
229    public void setRemovedUsers( List<User> removedUsers )
230    {
231        this.removedUsers = removedUsers;
232    }
233
234    @Override
235    public String toString()
236    {
237        final StringBuilder sb = new StringBuilder();
238        sb.append( "Role" );
239        sb.append( "{name='" ).append( name ).append( '\'' );
240        sb.append( ", description='" ).append( description ).append( '\'' );
241        sb.append( ", assignable=" ).append( assignable );
242        sb.append( ", childRoleNames=" ).append( childRoleNames );
243        sb.append( ", permissions=" ).append( permissions );
244        sb.append( ", parentRoleNames=" ).append( parentRoleNames );
245        sb.append( ", parentsRolesUsers=" ).append( parentsRolesUsers );
246        sb.append( ", users=" ).append( users );
247        sb.append( ", otherUsers=" ).append( otherUsers );
248        sb.append( ", removedUsers=" ).append( removedUsers );
249        sb.append( ", permanent=" ).append( permanent );
250        sb.append( '}' );
251        return sb.toString();
252    }
253
254
255    @Override
256    public boolean equals( Object o )
257    {
258        if ( this == o )
259        {
260            return true;
261        }
262        if ( o == null || getClass() != o.getClass() )
263        {
264            return false;
265        }
266
267        Role role = (Role) o;
268
269        if ( name != null ? !name.equals( role.name ) : role.name != null )
270        {
271            return false;
272        }
273
274        return true;
275    }
276
277    @Override
278    public int hashCode()
279    {
280        return name != null ? name.hashCode() : 0;
281    }
282}