package codeine.servlet;

import javax.inject.Inject;

import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.util.security.Credential;

import codeine.jsons.auth.CodeineUser;
import codeine.jsons.auth.IdentityConfJson;
import codeine.model.Constants;
import codeine.utils.ExceptionUtils;
import codeine.utils.JsonFileUtils;

public class UsersManager {

	private @Inject IdentityConfJson identityConfJson;
	private @Inject HashLoginService hashLoginService;
	private @Inject JsonFileUtils jsonFileUtils;
	
	private void store() {
		jsonFileUtils.setContent(Constants.getIdentityConfPath(), identityConfJson);
	}
	
	public LoginService loginService() {
		return hashLoginService;
	}

	public void initUsers() {
		for (CodeineUser user : identityConfJson.entries()) {
			user.api_token();
			putUser(user.username(), user.credentials());
		}
	}
	
	private void putUser(String name, String credentials) {
		hashLoginService.putUser(name, Credential.getCredential(credentials), new String[] { "user" });
	}

	public void addUser(String name, String credentials) {
		identityConfJson.add(name, credentials);
		store();
		putUser(name, credentials);
	}
	
	public CodeineUser userByApiToken(String api_token) {
		for (CodeineUser user : identityConfJson.entries()) {
			if (user.api_token().equals(api_token))
				return user;
		}
		throw ExceptionUtils.asUnchecked(new IllegalArgumentException("No such user with api token " + api_token));
	}
	
	public CodeineUser user(String name) {
		
		for (CodeineUser user : identityConfJson.entries()) {
			if (user.username().equals(name)) return user;
		}
		throw ExceptionUtils.asUnchecked(new IllegalArgumentException("No such user " + name));
	}
	
	public boolean isUserExists(String username) {
		return hashLoginService.getUsers().containsKey(username);
	}

	public boolean hasUsers() {
		return hashLoginService.getUsers().size() > 0;
	}
}

@@ -44,24 +44,56 @@
 ct.Inject;%0A%0A
+import org.apache.log4j.Logger;%0A
 import org.e
@@ -124,32 +124,32 @@
 shLoginService;%0A
-
 import org.eclip
@@ -232,16 +232,64 @@
 ntial;%0A%0A
+import codeine.jsons.auth.AuthenticationMethod;%0A
 import c
@@ -363,16 +363,74 @@
 nfJson;%0A
+import codeine.jsons.global.GlobalConfigurationJsonStore;%0A
 import c
@@ -556,16 +556,89 @@
 ager %7B%0A%0A
+%09private static final Logger log = Logger.getLogger(UsersManager.class);%0A
 %09private
@@ -779,16 +779,92 @@
 eUtils;%0A
+%09private @Inject GlobalConfigurationJsonStore globalConfigurationJsonStore;%0A
 %09%0A%09priva
@@ -1363,20 +1363,27 @@
 %09public 
-void
+CodeineUser
  addUser
@@ -1420,16 +1420,35 @@
 ls) %7B%0A%09%09
+CodeineUser user = 
 identity
@@ -1521,16 +1521,31 @@
 tials);%0A
+%09%09return user;%0A
 %09%7D%0A%09%0A%09pu
@@ -1875,11 +1875,8 @@
 ) %7B%0A
-%09%09%0A
 %09%09fo
@@ -1919,32 +1919,32 @@
 on.entries()) %7B%0A
-
 %09%09%09if (user.user
@@ -1973,32 +1973,230 @@
 eturn user;%0A%09%09%7D%0A
+%09%09if (globalConfigurationJsonStore.get().authentication_method() == AuthenticationMethod.WindowsCredentials) %7B%0A%09%09%09log.info(%22creating automaticly user %22 + name);%0A%09%09%09return addUser(name, %22NONE%22);%0A%09%09%7D%0A
 %09%09throw Exceptio

fix error in servlet