001package org.apache.archiva.redback.rest.api.model.v2;/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 * Unless required by applicable law or agreed to in writing,
012 * software distributed under the License is distributed on an
013 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
014 * KIND, either express or implied.  See the License for the
015 * specific language governing permissions and limitations
016 * under the License.
017 */
018
019import io.swagger.v3.oas.annotations.media.Schema;
020import org.apache.archiva.redback.rbac.Role;
021import org.apache.archiva.redback.role.model.ModelRole;
022
023import javax.xml.bind.annotation.XmlTransient;
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.List;
027
028/**
029 * Basic role information. This class contains only the standard attributes used for displaying a role.
030 *
031 * @author Martin Stockhammer <martin_s@apache.org>
032 */
033@Schema(name="BaseRoleInfo", description = "Basic role attributes")
034public class BaseRoleInfo implements Serializable
035{
036    private static final long serialVersionUID = -6725489773301720068L;
037
038    protected String id;
039    protected String name;
040    protected String description = "";
041    protected boolean permanent = false;
042    protected String modelId = "";
043    protected String resource = "";
044    protected boolean isTemplateInstance = false;
045    protected boolean assignable = true;
046    private String applicationId = "";
047    private boolean isChild = false;
048
049    protected boolean assigned = false;
050    private List<BaseRoleInfo> children = new ArrayList<>( 0 );
051
052    public BaseRoleInfo() {
053
054    }
055
056    public static BaseRoleInfo ofName(String name) {
057        BaseRoleInfo info = new BaseRoleInfo( );
058        info.setName( name );
059        return info;
060    }
061
062    public static BaseRoleInfo ofId(String id) {
063        BaseRoleInfo info = new BaseRoleInfo( );
064        info.setId( id );
065        return info;
066    }
067
068    public static BaseRoleInfo of(Role rbacRole) {
069        return of( rbacRole, new BaseRoleInfo( ) );
070    }
071
072
073    public static <T extends BaseRoleInfo>  T of( Role rbacRole, T role ) {
074        role.id = rbacRole.getId( );
075        role.name = rbacRole.getName( );
076        role.description = rbacRole.getDescription( ) == null ?"": rbacRole.getDescription();
077        role.permanent = rbacRole.isPermanent( );
078        role.modelId = rbacRole.getModelId( );
079        role.resource = rbacRole.getResource( );
080        role.isTemplateInstance = rbacRole.isTemplateInstance( );
081        role.assignable = rbacRole.isAssignable( );
082        return role;
083    }
084
085
086
087    @Schema(description = "The role name")
088    public String getName()
089    {
090        return name;
091    }
092
093    public void setName( String name )
094    {
095        this.name = name;
096    }
097
098    @Schema( description = "A description of the role" )
099    public String getDescription( )
100    {
101        return description;
102    }
103
104    public void setDescription( String description )
105    {
106        this.description = description;
107    }
108
109    @Schema( description = "True, if this role cannot be deleted.")
110    public boolean isPermanent()
111    {
112        return permanent;
113    }
114
115    public void setPermanent( boolean permanent )
116    {
117        this.permanent = permanent;
118    }
119
120    @Schema(description = "The identifier of this role")
121    public String getId( )
122    {
123        return id;
124    }
125
126    public void setId( String id )
127    {
128        this.id = id;
129    }
130
131    @Schema(name="model_id", description = "The model this role is derived from")
132    public String getModelId( )
133    {
134        return modelId;
135    }
136
137    public void setModelId( String modelId )
138    {
139        this.modelId = modelId;
140    }
141
142    @Schema(description = "The resource this model is built for, if it is built by a template.")
143    public String getResource( )
144    {
145        return resource;
146    }
147
148    public void setResource( String resource )
149    {
150        this.resource = resource;
151    }
152
153    @Schema(name="template_instance", description = "True, if this is a instance of a role template")
154    public boolean isTemplateInstance( )
155    {
156        return isTemplateInstance;
157    }
158
159    public void setTemplateInstance( boolean templateInstance )
160    {
161        isTemplateInstance = templateInstance;
162    }
163
164    @Schema(description = "Roles that are children of this role. This field may not be populated, depending on the REST method call.")
165    public List<BaseRoleInfo> getChildren( )
166    {
167        return children;
168    }
169
170    public void setChildren( List<BaseRoleInfo> children )
171    {
172        this.children = children;
173    }
174
175    public void addChild(BaseRoleInfo child) {
176        if (!this.children.contains( child ))
177        {
178            this.children.add( child );
179        }
180    }
181
182    @Schema(description = "This attribute is only set at specific REST calls, that return roles, that are either assigned or not assigned to a given user.")
183    public boolean isAssigned( )
184    {
185        return assigned;
186    }
187
188    public void setAssigned( boolean assigned )
189    {
190        this.assigned = assigned;
191    }
192
193
194    @Schema( description = "If true, the role is assignable to users or roles. Otherwise, it can be used only as parent role.")
195    public boolean isAssignable()
196    {
197        return assignable;
198    }
199
200    public void setAssignable( boolean assignable )
201    {
202        this.assignable = assignable;
203    }
204
205    @Override
206    public int hashCode( )
207    {
208        return id.hashCode( );
209    }
210
211    @Override
212    public String toString( )
213    {
214        final StringBuilder sb = new StringBuilder( "BaseRoleInfo{" );
215        sb.append( "id='" ).append( id ).append( '\'' );
216        sb.append( ", name='" ).append( name ).append( '\'' );
217        sb.append( ", description='" ).append( description ).append( '\'' );
218        sb.append( ", permanent=" ).append( permanent );
219        sb.append( ", modelId='" ).append( modelId ).append( '\'' );
220        sb.append( ", resource='" ).append( resource ).append( '\'' );
221        sb.append( ", isTemplateInstance=" ).append( isTemplateInstance );
222        sb.append( '}' );
223        return sb.toString( );
224    }
225
226    @Schema(name="application_id", description = "Application id, where this role belongs to. This is only filled by certain REST methods.")
227    public String getApplicationId( )
228    {
229        return applicationId;
230    }
231
232    public void setApplicationId( String applicationId )
233    {
234        this.applicationId = applicationId;
235    }
236
237    public boolean isChild( )
238    {
239        return isChild;
240    }
241
242    public void setChild( boolean child )
243    {
244        isChild = child;
245    }
246
247    @XmlTransient
248    public boolean isNotChild() {
249        return !isChild;
250    }
251
252    @Override
253    public boolean equals( Object o )
254    {
255        if ( this == o ) return true;
256        if ( o == null || getClass( ) != o.getClass( ) ) return false;
257
258        BaseRoleInfo that = (BaseRoleInfo) o;
259
260        if ( permanent != that.permanent ) return false;
261        if ( isTemplateInstance != that.isTemplateInstance ) return false;
262        if ( assignable != that.assignable ) return false;
263        if ( !id.equals( that.id ) ) return false;
264        if ( !name.equals( that.name ) ) return false;
265        if ( description != null ? !description.equals( that.description ) : that.description != null ) return false;
266        if ( modelId != null ? !modelId.equals( that.modelId ) : that.modelId != null ) return false;
267        if ( resource != null ? !resource.equals( that.resource ) : that.resource != null ) return false;
268        return applicationId != null ? applicationId.equals( that.applicationId ) : that.applicationId == null;
269    }
270}