1/*
2 * Copyright 2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.fake.command
17
18import org.mockftpserver.fake.command.AbstractFakeCommandHandler
19import org.mockftpserver.core.command.Command
20import org.mockftpserver.core.session.Session
21import org.mockftpserver.core.session.SessionKeys
22import org.mockftpserver.core.command.ReplyCodes
23
24/**
25 * CommandHandler for the USER command. Handler logic:
26 * <ol>
27 *  <li>If the required pathname parameter is missing, then reply with 501</li>
28 *  <li>If the named user does not need a password for login, then reply with 230</li>
29 *  <li>Otherwise, reply with 331</li>
30 * </ol>
31 *
32 * @version $Revision: $ - $Date: $
33 *
34 * @author Chris Mair
35 */
36class UserCommandHandler extends AbstractFakeCommandHandler {
37
38    protected void handle(Command command, Session session) {
39        def username = getRequiredParameter(command)
40
41        // If the UserAccount is configured to not require password for login
42        def userAccount = serverConfiguration.getUserAccount(username)
43        if (userAccount && !userAccount.passwordRequiredForLogin) {
44            session.setAttribute(SessionKeys.USER_ACCOUNT, userAccount)
45            sendReply(session, ReplyCodes.USER_LOGGED_IN_OK)
46        }
47        else {
48            session.setAttribute(SessionKeys.USERNAME, username)
49            sendReply(session, ReplyCodes.USER_NEED_PASSWORD_OK)
50        }
51    }
52
53}