001    /*
002     * ----------------------------------------------------------------------
003     * Copyright (C) 2009 Enrique Lara (k957@68k.org)
004     *
005     * TinLizard is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public License
007     * as published by the Free Software Foundation; either version 3.0
008     * of the License, or (at your option) any later version.
009     *
010     * TinLizard is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013     * GNU Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public License
016     * along with TinLizard. If not, see http://www.gnu.org/licenses/.
017     * ----------------------------------------------------------------------
018     */
019    package tinlizard.web;
020    
021    import tinlizard.annotation.security.RolesAllowed;
022    
023    import tinlizard.model.Codeline;
024    import tinlizard.model.User;
025    
026    import tinlizard.util.Messages;
027    
028    import java.util.Collection;
029    import java.util.Date;
030    
031    import javax.servlet.http.HttpServletResponse;
032    
033    import org.codehaus.plexus.util.StringUtils;
034    
035    import org.kohsuke.stapler.StaplerRequest;
036    import org.kohsuke.stapler.StaplerResponse;
037    import org.kohsuke.stapler.export.Exported;
038    
039    /**
040     * Decorate User for Stapler.
041     */
042    public final class UserWeb extends ObjectWeb<User> {
043        private final User subject;
044    
045        public UserWeb(final User user) {
046            if (user == null) {
047                throw new IllegalStateException(Messages.error_1016());
048            }
049    
050            this.subject = user;
051        }
052    
053        protected User getSubject() {
054            return this.subject;
055        }
056    
057        public Integer getId() {
058            return subject.getId();
059        }
060    
061        public String getName() {
062            return subject.getName();
063        }
064    
065        @Exported
066        public String getEmail() {
067            return subject.getEmail();
068        }
069    
070        @Exported
071        public Date getCreated() {
072            return subject.getCreated();
073        }
074    
075        @Exported
076        public String getCreatedBy() {
077            return subject.getCreatedBy();
078        }
079    
080        @Exported
081        public Date getLastModified() {
082            return subject.getLastModified();
083        }
084    
085        @Exported
086        public String getLastModifiedBy() {
087            return subject.getLastModifiedBy();
088        }
089    
090        @Exported(visibility = 1)
091        public CodelineCollectionWeb getCodelines() {
092            Collection<Codeline> codelines = this.subject.getCodelines();
093    
094            if (codelines != null) {
095                return new CodelineCollectionWeb(codelines, Messages._Codelines());
096            } else {
097                return null;
098            }
099        }
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    }