001    /* 
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    
012        Unless required by applicable law or agreed to in writing,
013        software distributed under the License is distributed on an
014        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015        KIND, either express or implied.  See the License for the
016        specific language governing permissions and limitations
017        under the License.  
018     */
019    package org.apache.wiki.ui.admin.beans;
020    
021    import java.util.Date;
022    
023    import javax.management.NotCompliantMBeanException;
024    import javax.servlet.http.HttpServletRequest;
025    
026    import org.apache.wiki.WikiContext;
027    import org.apache.wiki.WikiEngine;
028    import org.apache.wiki.WikiSession;
029    import org.apache.wiki.auth.NoSuchPrincipalException;
030    import org.apache.wiki.auth.UserManager;
031    import org.apache.wiki.auth.WikiSecurityException;
032    import org.apache.wiki.auth.user.UserProfile;
033    import org.apache.wiki.ui.admin.AdminBean;
034    import org.apache.wiki.ui.admin.SimpleAdminBean;
035    
036    
037    public class UserBean extends SimpleAdminBean
038    {
039        public UserBean( WikiEngine engine ) throws NotCompliantMBeanException
040        {
041            super();
042        }
043    
044        public String[] getAttributeNames()
045        {
046            return new String[0];
047        }
048    
049        // FIXME: We don't yet support MBean for this kind of stuff.
050        public String[] getMethodNames()
051        {
052            return new String[0];
053        }
054    
055    
056    
057        public String doPost(WikiContext context)
058        {
059            HttpServletRequest request = context.getHttpRequest();
060            WikiSession session = context.getWikiSession();
061            UserManager mgr = context.getEngine().getUserManager();
062    
063            String loginid   = request.getParameter("loginid");
064            String loginname = request.getParameter("loginname");
065            String fullname  = request.getParameter("fullname");
066            String password  = request.getParameter("password");
067            String password2 = request.getParameter("password2");
068            String email     = request.getParameter("email");
069    
070    
071            if( request.getParameter("action").equalsIgnoreCase("remove") )
072            {
073                try
074                {
075                    mgr.getUserDatabase().deleteByLoginName(loginid);
076                    session.addMessage("User profile "+loginid+" ("+fullname+") has been deleted");
077                }
078                catch (NoSuchPrincipalException e)
079                {
080                    session.addMessage("User profile has already been removed");
081                }
082                catch (WikiSecurityException e)
083                {
084                    session.addMessage("Security problem: "+e);
085                }
086                return "";
087            }
088    
089    
090            if( password != null && password.length() > 0 && !password.equals(password2) )
091            {
092                session.addMessage("Passwords do not match!");
093                return "";
094            }
095    
096            UserProfile p;
097    
098            if( loginid.equals("--New--") )
099            {
100                // Create new user
101    
102                p = mgr.getUserDatabase().newProfile();
103                p.setCreated( new Date() );
104            }
105            else
106            {
107                try
108                {
109                    p = mgr.getUserDatabase().findByLoginName( loginid );
110                }
111                catch (NoSuchPrincipalException e)
112                {
113                    session.addMessage("I could not find user profile "+loginid);
114                    return "";
115                }
116            }
117    
118            p.setEmail(email);
119            p.setFullname(fullname);
120            if( password != null && password.length() > 0 ) p.setPassword(password);
121            p.setLoginName(loginname);
122    
123            try
124            {
125                mgr.getUserDatabase().save( p );
126            }
127            catch( WikiSecurityException e )
128            {
129                session.addMessage("Unable to save "+e.getMessage());
130            }
131    
132            session.addMessage("User profile has been updated");
133    
134            return "";
135        }
136    
137        public String getTitle()
138        {
139            return "User administration";
140        }
141    
142        public int getType()
143        {
144            return AdminBean.UNKNOWN;
145        }
146    
147    }