All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.appslandia.plum.base.MemFormController Maven / Gradle / Ivy

// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.appslandia.plum.base;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;

import com.appslandia.common.base.Out;
import com.appslandia.common.utils.AssertUtils;
import com.appslandia.plum.models.LoginModel;

/**
 *
 * @author Loc Ha
 *
 */
@ApplicationScoped
@Controller(module = "memForm")
public class MemFormController {

	@Inject
	protected AuthContext authContext;

	@Inject
	protected MemUserDatabase memUserDatabase;

	@HttpGet
	@EnableGzip(removed = true)
	public Result index() {
		return new Result().setMessage("index");
	}

	@HttpGet
	@FormLogin
	@EnableGzip(removed = true)
	public Result login(RequestAccessor request, HttpServletResponse response, @Model LoginModel model) throws Exception {
		request.getModelState().clearErrors();

		if (request.hasPrincipalForCurrentModule()) {
			model.setUserName(request.getRemoteUser());
			model.setRememberMe(request.getUserPrincipal().isRememberMe());
		}

		if (model.getUserName() == null) {
			request.getModelState().addError("userName", "userName is required.");
		}
		if (model.getPassword() == null) {
			request.getModelState().addError("password", "password is required.");
		}

		if (!request.getModelState().isValid()) {
			return new Result().asError().setMessage("Model is invalid.");
		}

		AuthParameters authParameters = new AuthParameters().credential(new MemUserPasswordCredential(model.getUserName(), model.getPassword())).rememberMe(model.isRememberMe())
				.reauthentication(request.hasPrincipalForCurrentModule());

		if (request.hasPrincipal()) {
			MemUser user = this.memUserDatabase.getUser(model.getUserName());
			AssertUtils.assertNotNull(user);

			if (!this.memUserDatabase.verifyPassword(model.getPassword(), user.getPassword())) {
				request.getModelState().addError("password", "The password is incorrect.");
				return new Result().asError();
			}

			// LOGOUT
			request.logout();
		}

		// Authenticate
		Out invalidCode = new Out<>();
		if (!this.authContext.authenticate(request, response, authParameters, invalidCode)) {
			return new Result().asError().setMessage(invalidCode.get());
		}
		return new Result().setMessage("logged in successfully.");
	}

	@HttpGet
	public Result logout(RequestAccessor request, HttpServletResponse response) throws Exception {
		if (request.getUserPrincipal() != null) {
			request.logout();
		}
		return new Result().setMessage("logged out successfully.");
	}

	@HttpGet
	@Authorize
	@EnableGzip(removed = true)
	public Result test() {
		return new Result().setMessage("test");
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy