View Javadoc

1   /*
2    * ----------------------------------------------------------------------
3    * Copyright (C) 2009 Enrique Lara (k957@68k.org)
4    *
5    * TinLizard is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public License
7    * as published by the Free Software Foundation; either version 3.0
8    * of the License, or (at your option) any later version.
9    *
10   * TinLizard is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with TinLizard. If not, see http://www.gnu.org/licenses/.
17   * ----------------------------------------------------------------------
18   */
19  package tinlizard.web;
20  
21  import tinlizard.annotation.security.RolesAllowed;
22  
23  import tinlizard.model.Codeline;
24  import tinlizard.model.User;
25  
26  import tinlizard.util.Messages;
27  
28  import java.util.Collection;
29  import java.util.Date;
30  
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.codehaus.plexus.util.StringUtils;
34  
35  import org.kohsuke.stapler.StaplerRequest;
36  import org.kohsuke.stapler.StaplerResponse;
37  import org.kohsuke.stapler.export.Exported;
38  
39  /***
40   * Decorate User for Stapler.
41   */
42  public final class UserWeb extends ObjectWeb<User> {
43      private final User subject;
44  
45      public UserWeb(final User user) {
46          if (user == null) {
47              throw new IllegalStateException(Messages.error_1016());
48          }
49  
50          this.subject = user;
51      }
52  
53      protected User getSubject() {
54          return this.subject;
55      }
56  
57      public Integer getId() {
58          return subject.getId();
59      }
60  
61      public String getName() {
62          return subject.getName();
63      }
64  
65      @Exported
66      public String getEmail() {
67          return subject.getEmail();
68      }
69  
70      @Exported
71      public Date getCreated() {
72          return subject.getCreated();
73      }
74  
75      @Exported
76      public String getCreatedBy() {
77          return subject.getCreatedBy();
78      }
79  
80      @Exported
81      public Date getLastModified() {
82          return subject.getLastModified();
83      }
84  
85      @Exported
86      public String getLastModifiedBy() {
87          return subject.getLastModifiedBy();
88      }
89  
90      @Exported(visibility = 1)
91      public CodelineCollectionWeb getCodelines() {
92          Collection<Codeline> codelines = this.subject.getCodelines();
93  
94          if (codelines != null) {
95              return new CodelineCollectionWeb(codelines, Messages._Codelines());
96          } else {
97              return null;
98          }
99      }
100 
101     @RolesAllowed(RoleNames.ADMIN)
102     public void doDelete(final StaplerRequest request, final StaplerResponse response) throws Exception {
103         subject.delete();
104         response.sendRedirect(request.getContextPath());
105     }
106 
107     //XXX prevent updating id?
108     @RolesAllowed(RoleNames.USER)
109     public void doUpdate(final StaplerRequest request, final StaplerResponse response) throws Exception {
110         if (StringUtils.equals(request.getUserPrincipal().getName(), subject.getName()) || request.isUserInRole(RoleNames.ADMIN)) {
111             request.bindParameters(subject, "user.");
112             subject.update();
113             gotoMyIndex(request, response);
114         } else {
115             response.sendError(HttpServletResponse.SC_FORBIDDEN);
116         }
117     }
118 }