]> source.dussan.org Git - archiva.git/blob
18cd5eca3cc272a5c4c7b4e3526d753b79ae6530
[archiva.git] /
1 package org.codehaus.plexus.redback.struts2.action.admin;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.archiva.redback.users.User;
23 import org.apache.archiva.redback.users.UserManager;
24 import org.codehaus.plexus.redback.rbac.RBACManager;
25 import org.codehaus.plexus.redback.rbac.RbacManagerException;
26 import org.codehaus.plexus.redback.rbac.RbacObjectInvalidException;
27 import org.codehaus.plexus.redback.rbac.RbacObjectNotFoundException;
28 import org.codehaus.plexus.redback.rbac.Resource;
29 import org.codehaus.plexus.redback.struts2.action.AbstractSecurityAction;
30 import org.codehaus.plexus.redback.struts2.action.AuditEvent;
31 import org.codehaus.plexus.redback.struts2.action.CancellableAction;
32 import org.apache.archiva.redback.users.UserNotFoundException;
33 import org.codehaus.plexus.util.StringUtils;
34 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
35 import org.codehaus.redback.integration.interceptor.SecureActionException;
36 import org.codehaus.redback.integration.role.RoleConstants;
37 import org.springframework.context.annotation.Scope;
38 import org.springframework.stereotype.Controller;
39
40 import javax.inject.Inject;
41 import javax.inject.Named;
42 import java.util.Arrays;
43
44 /**
45  * UserDeleteAction
46  *
47  * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
48  * @version $Id$
49  */
50 @Controller( "redback-admin-user-delete" )
51 @Scope( "prototype" )
52 public class UserDeleteAction
53     extends AbstractSecurityAction
54     implements CancellableAction
55 {
56     // ------------------------------------------------------------------
57     // Component Requirements
58     // ------------------------------------------------------------------
59
60     /**
61      *  role-hint="configurable"
62      */
63     @Inject
64     @Named( value = "userManager#configurable" )
65     private UserManager userManager;
66
67     /**
68      *  role-hint="cached"
69      */
70     @Inject
71     @Named( value = "rBACManager#cached" )
72     private RBACManager rbacManager;
73
74     // ------------------------------------------------------------------
75     // Action Parameters
76     // ------------------------------------------------------------------
77
78     private String username;
79
80     private User user;
81
82     // ------------------------------------------------------------------
83     // Action Entry Points - (aka Names)
84     // ------------------------------------------------------------------
85
86     public String confirm()
87     {
88         if ( username == null )
89         {
90             addActionError( getText( "cannot.remove.user.null.username" ) );
91             return SUCCESS;
92         }
93
94         try
95         {
96             user = userManager.findUser( username );
97         }
98         catch ( UserNotFoundException e )
99         {
100             addActionError( getText( "cannot.remove.user.not.found", Arrays.asList( (Object) username ) ) );
101             return SUCCESS;
102         }
103
104         return INPUT;
105     }
106
107     public String submit()
108     {
109         if ( username == null )
110         {
111             addActionError( getText( "invalid.user.credentials" ) );
112             return SUCCESS;
113         }
114
115         if ( StringUtils.isEmpty( username ) )
116         {
117             addActionError( getText( "cannot.remove.user.empty.username" ) );
118             return SUCCESS;
119         }
120
121         try
122         {
123             rbacManager.removeUserAssignment( username );
124         }
125         catch ( RbacObjectNotFoundException e )
126         {
127             // ignore, this is possible since the user may never have had roles assigned
128         }
129         catch ( RbacObjectInvalidException e )
130         {
131             addActionError( getText( "cannot.remove.user.role", Arrays.asList( (Object) username, e.getMessage() ) ) );
132         }
133         catch ( RbacManagerException e )
134         {
135             addActionError( getText( "cannot.remove.user.role", Arrays.asList( (Object) username, e.getMessage() ) ) );
136         }
137
138         if ( getActionErrors().isEmpty() )
139         {
140             try
141             {
142                 userManager.deleteUser( username );
143             }
144             catch ( UserNotFoundException e )
145             {
146                 addActionError( getText( "cannot.remove.user.non.existent", Arrays.asList( (Object) username ) ) );
147             }
148         }
149         String currentUser = getCurrentUser();
150
151         AuditEvent event = new AuditEvent( getText( "log.account.delete" ) );
152         event.setAffectedUser( username );
153         event.setCurrentUser( currentUser );
154         event.log();
155
156         return SUCCESS;
157     }
158
159     /**
160      * Returns the cancel result. <p/> A basic implementation would simply be to return CANCEL.
161      *
162      * @return
163      */
164     public String cancel()
165     {
166         return CANCEL;
167     }
168
169     // ------------------------------------------------------------------
170     // Parameter Accessor Methods
171     // ------------------------------------------------------------------
172
173     public String getUsername()
174     {
175         return username;
176     }
177
178     public void setUsername( String username )
179     {
180         this.username = username;
181     }
182
183     public User getUser()
184     {
185         return user;
186     }
187
188     public void setUser( User user )
189     {
190         this.user = user;
191     }
192
193     public SecureActionBundle initSecureActionBundle()
194         throws SecureActionException
195     {
196         SecureActionBundle bundle = new SecureActionBundle();
197         bundle.setRequiresAuthentication( true );
198         bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_USER_DELETE_OPERATION, Resource.GLOBAL );
199         return bundle;
200     }
201
202 }