001package org.apache.archiva.redback.rest.api.model.v2;
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
021
022import io.swagger.v3.oas.annotations.media.Schema;
023import org.apache.archiva.redback.rbac.Role;
024
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlTransient;
027import java.io.Serializable;
028import java.util.ArrayList;
029import java.util.List;
030import java.util.Objects;
031import java.util.stream.Collectors;
032
033/**
034 * Result object for role information.
035 *
036 * @author Martin Stockhammer
037 * @since 3.0
038 */
039@XmlRootElement( name = "role" )
040@Schema(name="RoleInfo",description = "Information about role.")
041public class RoleInfo extends BaseRoleInfo
042    implements Serializable
043{
044    private static final long serialVersionUID = -3506615158923923845L;
045
046    /**
047     * Field childRoleNames
048     */
049    private List<String> childRoleIds = new ArrayList<>(0);
050
051    /**
052     * Field permissions
053     */
054    private List<Permission> permissions = new ArrayList<>(0);
055
056    /**
057     * The names of all parent roles
058     */
059    private List<String> parentRoleIds = new ArrayList<>(0);
060
061    public RoleInfo()
062    {
063        // no op
064    }
065
066
067    public static RoleInfo of( Role rbacRole) {
068        RoleInfo role = BaseRoleInfo.of( rbacRole, new RoleInfo( ) );
069        if(rbacRole.getPermissions()!=null)
070        {
071            role.permissions = rbacRole.getPermissions( ).stream( ).map( rbacPerm ->
072                Permission.of( rbacPerm )
073            ).collect( Collectors.toList( ) );
074        }
075        return role;
076    }
077
078    @XmlTransient
079    @Override
080    public List<BaseRoleInfo> getChildren( )
081    {
082        return super.getChildren( );
083    }
084
085    @Schema( name="child_role_ids", description = "List of names of children roles")
086    public List<String> getChildRoleIds()
087    {
088        return childRoleIds;
089    }
090
091    public void setChildRoleIds( List<String> childRoleIds )
092    {
093        this.childRoleIds = childRoleIds;
094    }
095
096    @Schema( description = "List of permissions assigned to this role.")
097    public List<Permission> getPermissions()
098    {
099        return permissions;
100    }
101
102    public void setPermissions( List<Permission> permissions )
103    {
104        this.permissions = permissions;
105    }
106
107    @Schema(name="parent_role_ids", description = "List of names of roles that are parents of this role.")
108    public List<String> getParentRoleIds()
109    {
110        return parentRoleIds;
111    }
112
113    public void setParentRoleIds( List<String> parentRoleIds )
114    {
115        this.parentRoleIds = parentRoleIds;
116    }
117
118    @Override
119    public boolean isChild( )
120    {
121        return getParentRoleIds( ).size( ) > 0;
122    }
123
124    @Override
125    public int hashCode()
126    {
127        return getName( ) != null ? getName( ).hashCode() : 0;
128    }
129
130    @Override
131    public String toString( )
132    {
133        final StringBuilder sb = new StringBuilder( "RoleInfo{" );
134        sb.append( "name='" ).append( getName( ) ).append( '\'' );
135        sb.append( ", id='" ).append( getId( ) ).append( '\'' );
136        sb.append( ", description='" ).append( getDescription( ) ).append( '\'' );
137        sb.append( ", assignable=" ).append( assignable );
138        sb.append( ", childRoleNames=" ).append( childRoleIds );
139        sb.append( ", permissions=" ).append( permissions );
140        sb.append( ", parentRoleNames=" ).append( parentRoleIds );
141        sb.append( ", permanent=" ).append( isPermanent( ) );
142        sb.append( ", modelId='" ).append( modelId ).append( '\'' );
143        sb.append( ", resource='" ).append( resource ).append( '\'' );
144        sb.append( ", isTemplateInstance=" ).append( isTemplateInstance );
145        sb.append( '}' );
146        return sb.toString( );
147    }
148
149
150
151
152}