package com.sixsq.slipstream.authn;

/*
 * +=================================================================+
 * SlipStream Server (WAR)
 * =====
 * Copyright (C) 2013 SixSq Sarl (sixsq.com)
 * =====
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * -=================================================================-
 */

import static com.sixsq.slipstream.messages.MessageUtils.MSG_ACCOUNT_APPROVED;
import static com.sixsq.slipstream.messages.MessageUtils.MSG_EMAIL_CONFIRMED;
import static com.sixsq.slipstream.messages.MessageUtils.MSG_NEW_USER_NOTIFICATION;

import javax.persistence.Entity;

import org.restlet.Request;
import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;

import com.sixsq.slipstream.configuration.Configuration;
import com.sixsq.slipstream.exceptions.ConfigurationException;
import com.sixsq.slipstream.exceptions.SlipStreamRuntimeException;
import com.sixsq.slipstream.messages.MessageUtils;
import com.sixsq.slipstream.persistence.OneShotAction;
import com.sixsq.slipstream.persistence.ServiceConfiguration;
import com.sixsq.slipstream.persistence.ServiceConfigurationParameter;
import com.sixsq.slipstream.persistence.User;
import com.sixsq.slipstream.util.RequestUtil;

@Entity
public class UserEmailValidationAction extends OneShotAction {

	@SuppressWarnings("unused")
	private UserEmailValidationAction() {
	}

	public UserEmailValidationAction(String userResourceUrl) {
		super();

		Form form = new Form();
		form.add("userResourceUrl", userResourceUrl);
		setForm(form);
	}

	protected void emailValidated(String baseUrlSlash)
			throws SlipStreamRuntimeException, ConfigurationException {

		Form form = getForm();
		String userResourceUrl = form.getFirst("userResourceUrl").getValue();

		User user = User.load(userResourceUrl);
		user.setState(User.State.ACTIVE);

		user.store();

		informUserAccountActivated(user, baseUrlSlash, userResourceUrl);

		informAdministratorOfUserCreation(baseUrlSlash, userResourceUrl);
	}

	private void informUserAccountActivated(User user, String baseUrlSlash,
			String userResourceUrl) {
		String username = user.getName();
		String password = user.getPassword();
		String url = baseUrlSlash + userResourceUrl;

		String msg = MessageUtils.format(MSG_ACCOUNT_APPROVED, username,
				password, url);

		Notifier.sendNotification(user.getEmail(), msg);
	}

	private void informAdministratorOfUserCreation(String baseUrlSlash,
			String userResourceUrl) throws ConfigurationException {

		String msg = MessageUtils.format(MSG_NEW_USER_NOTIFICATION,
				baseUrlSlash + userResourceUrl);

		Notifier.sendNotification(getRegistrationEmail(), msg);
	}

	private String getRegistrationEmail() throws ConfigurationException {
		ServiceConfigurationParameter p = Configuration
				.getInstance()
				.getParameters()
				.getParameter(
						ServiceConfiguration.RequiredParameters.SLIPSTREAM_REGISTRATION_EMAIL
								.getName());
		return p.getValue();
	}

	@Override
	public Representation doAction(Request request)
			throws SlipStreamRuntimeException, ConfigurationException {

		String baseUrlSlash = RequestUtil.getBaseUrlSlash(request);

		emailValidated(baseUrlSlash);

		String msg = MessageUtils.format(MSG_EMAIL_CONFIRMED);

		return new StringRepresentation(msg);
	}
}

@@ -1070,16 +1070,109 @@
 ATION;%0A%0A
+import java.io.UnsupportedEncodingException;%0Aimport java.security.NoSuchAlgorithmException;%0A%0A
 import j
@@ -2199,15 +2199,13 @@
 %0A%09pr
-otec
+iva
 te
-d
  voi
@@ -2489,23 +2489,15 @@
 %0A%0A%09%09
-user.store();%0A%0A
+try %7B%0A%09
 %09%09in
@@ -2554,24 +2554,244 @@
 esourceUrl);
+%0A%09%09%7D catch (NoSuchAlgorithmException e) %7B%0A%09%09%09throw new ConfigurationException(e.getMessage());%0A%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09throw new ConfigurationException(e.getMessage());%0A%09%09%7D%0A%0A%09%09user = user.store();
 %0A%0A%09%09informAd
@@ -2947,24 +2947,89 @@
 ResourceUrl)
+ throws NoSuchAlgorithmException,%0A%09%09%09UnsupportedEncodingException
  %7B%0A%09%09String 
@@ -3227,24 +3227,167 @@
 ord, url);%0A%0A
+%09%09// now that the random password has been sent to the user%0A%09%09// hash it before it is stored%0A%09%09user.hashAndSetPassword(user.getPassword());%0A%09%09%0A
 %09%09Notifier.s

Add hashing of password once account activated