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 */
019package org.apache.wiki.ui.admin.beans;
020
021import org.apache.wiki.api.core.Context;
022import org.apache.wiki.api.core.Engine;
023import org.apache.wiki.api.core.Session;
024import org.apache.wiki.auth.NoSuchPrincipalException;
025import org.apache.wiki.auth.UserManager;
026import org.apache.wiki.auth.WikiSecurityException;
027import org.apache.wiki.auth.user.UserProfile;
028import org.apache.wiki.ui.admin.AdminBean;
029import org.apache.wiki.ui.admin.SimpleAdminBean;
030
031import javax.management.NotCompliantMBeanException;
032import javax.servlet.http.HttpServletRequest;
033import java.util.Date;
034
035
036public class UserBean extends SimpleAdminBean {
037
038    public UserBean( final Engine engine ) throws NotCompliantMBeanException  {
039        super();
040    }
041
042    @Override
043    public String[] getAttributeNames()
044    {
045        return new String[0];
046    }
047
048    // FIXME: We don't yet support MBean for this kind of stuff.
049    @Override
050    public String[] getMethodNames()
051    {
052        return new String[0];
053    }
054
055    @Override
056    public String doPost( final Context context ) {
057        final HttpServletRequest request = context.getHttpRequest();
058        final Session session = context.getWikiSession();
059        final UserManager mgr = context.getEngine().getManager( UserManager.class );
060        final String loginid = request.getParameter( "loginid" );
061        final String loginname = request.getParameter( "loginname" );
062        final String fullname = request.getParameter( "fullname" );
063        final String password = request.getParameter( "password" );
064        final String password2 = request.getParameter( "password2" );
065        final String email = request.getParameter( "email" );
066
067        if( request.getParameter( "action" ).equalsIgnoreCase( "remove" ) ) {
068            try {
069                mgr.getUserDatabase().deleteByLoginName( loginid );
070                session.addMessage( "User profile " + loginid + " (" + fullname + ") has been deleted" );
071            } catch( final NoSuchPrincipalException e ) {
072                session.addMessage( "User profile has already been removed" );
073            } catch( final WikiSecurityException e ) {
074                session.addMessage( "Security problem: " + e );
075            }
076            return "";
077        }
078
079        if( password != null && password.length() > 0 && !password.equals( password2 ) ) {
080            session.addMessage( "Passwords do not match!" );
081            return "";
082        }
083
084        final UserProfile p;
085
086        if( loginid.equals( "--New--" ) ) {
087            // Create new user
088
089            p = mgr.getUserDatabase().newProfile();
090            p.setCreated( new Date() );
091        } else {
092            try {
093                p = mgr.getUserDatabase().findByLoginName( loginid );
094            } catch( final NoSuchPrincipalException e ) {
095                session.addMessage( "I could not find user profile " + loginid );
096                return "";
097            }
098        }
099
100        p.setEmail( email );
101        p.setFullname( fullname );
102        if( password != null && !password.isEmpty() ) {
103            p.setPassword( password );
104        }
105        p.setLoginName( loginname );
106
107        try {
108            mgr.getUserDatabase().save( p );
109        } catch( final WikiSecurityException e ) {
110            session.addMessage( "Unable to save " + e.getMessage() );
111        }
112
113        session.addMessage("User profile has been updated");
114
115        return "";
116    }
117
118    @Override
119    public String getTitle() {
120        return "User administration";
121    }
122
123    @Override
124    public int getType() {
125        return AdminBean.UNKNOWN;
126    }
127
128}