This commit is contained in:
parent
b523f680f9
commit
57f0164e38
|
@ -1,8 +1,10 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<PrivilegeRoles>
|
<PrivilegeRoles>
|
||||||
|
|
||||||
|
<Role name="PrivilegeAdmin" />
|
||||||
<Role name="admin">
|
<Role name="admin">
|
||||||
<Privilege name="Service" />
|
<Privilege name="Service" />
|
||||||
</Role>
|
</Role>
|
||||||
|
<Role name="user" />
|
||||||
|
|
||||||
</PrivilegeRoles>
|
</PrivilegeRoles>
|
|
@ -33,6 +33,7 @@ public class PrivilegeContainer {
|
||||||
* This is the role users must have, if they can modify the {@link PrivilegeContainer} and its objects
|
* This is the role users must have, if they can modify the {@link PrivilegeContainer} and its objects
|
||||||
*/
|
*/
|
||||||
public static final String PRIVILEGE_ADMIN_ROLE = "PrivilegeAdmin";
|
public static final String PRIVILEGE_ADMIN_ROLE = "PrivilegeAdmin";
|
||||||
|
public static final String PRIVILEGE_CONTAINER_FILE = "PrivilegeContainer.xml";
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(PrivilegeContainer.class);
|
private static final Logger logger = Logger.getLogger(PrivilegeContainer.class);
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,12 @@ package ch.eitchnet.privilege.base;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class XmlConstants {
|
public class XmlConstants {
|
||||||
|
public static final String XML_ROOT_PRIVILEGE_CONTAINER = "PrivilegeContainer";
|
||||||
|
public static final String XML_ROOT_PRIVILEGE_ROLES = "PrivilegeRoles";
|
||||||
|
public static final String XML_ROOT_PRIVILEGES = "Privileges";
|
||||||
|
public static final String XML_ROOT_PRIVILEGE_USERS = "PrivilegesUsers";
|
||||||
|
public static final String XML_ROOT_RESTRICTION_POLICIES = "RestrictionPolicies";
|
||||||
|
|
||||||
public static final String XML_HANDLER_PERSISTENCE = "PersistenceHandler";
|
public static final String XML_HANDLER_PERSISTENCE = "PersistenceHandler";
|
||||||
public static final String XML_HANDLER_ENCRYPTION = "EncryptionHandler";
|
public static final String XML_HANDLER_ENCRYPTION = "EncryptionHandler";
|
||||||
public static final String XML_HANDLER_SESSION = "SessionHandler";
|
public static final String XML_HANDLER_SESSION = "SessionHandler";
|
||||||
|
|
|
@ -195,7 +195,7 @@ public class DefaultModelHandler implements ModelHandler {
|
||||||
|
|
||||||
// create new user
|
// create new user
|
||||||
Set<String> newRoles = new HashSet<String>(currentRoles);
|
Set<String> newRoles = new HashSet<String>(currentRoles);
|
||||||
currentRoles.add(roleName);
|
newRoles.add(roleName);
|
||||||
|
|
||||||
User newUser = new User(user.getUsername(), user.getPassword(certificate), user.getFirstname(), user
|
User newUser = new User(user.getUsername(), user.getPassword(certificate), user.getFirstname(), user
|
||||||
.getSurname(), user.getState(), newRoles, user.getLocale());
|
.getSurname(), user.getState(), newRoles, user.getLocale());
|
||||||
|
|
|
@ -0,0 +1,179 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2010
|
||||||
|
*
|
||||||
|
* Robert von Burg
|
||||||
|
* eitch@eitchnet.ch
|
||||||
|
*
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package ch.eitchnet.privilege.helper;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import org.apache.log4j.BasicConfigurator;
|
||||||
|
import org.apache.log4j.ConsoleAppender;
|
||||||
|
import org.apache.log4j.Level;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.apache.log4j.PatternLayout;
|
||||||
|
import org.dom4j.Document;
|
||||||
|
import org.dom4j.DocumentFactory;
|
||||||
|
import org.dom4j.Element;
|
||||||
|
|
||||||
|
import ch.eitchnet.privilege.base.PrivilegeContainer;
|
||||||
|
import ch.eitchnet.privilege.base.XmlConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* This class is a simple application which can be used to bootstrap a new configuration for the
|
||||||
|
* {@link PrivilegeContainer}
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Simple execute the application and it will ask a few questions and then write a new set of configuration files which
|
||||||
|
* can be used to run the {@link PrivilegeContainer}
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author rvonburg
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class BootstrapConfigurationHelper {
|
||||||
|
// private static final Logger logger = Logger.getLogger(BootstrapConfigurationHelper.class);
|
||||||
|
|
||||||
|
private static String path;
|
||||||
|
|
||||||
|
private static String usersFileName = "PrivilegeUsers.xml";
|
||||||
|
private static String rolesFileName = "PrivilegeRoles.xml";
|
||||||
|
private static String privilegesFileName = "Privileges.xml";
|
||||||
|
|
||||||
|
private static String hashAlgorithm = "SHA-256";
|
||||||
|
|
||||||
|
private static String policyXmlFile = "RestrictionPolicies.xml";
|
||||||
|
|
||||||
|
private static String defaultPersistenceHandler = "ch.eitchnet.privilege.handler.DefaultPersistenceHandler";
|
||||||
|
private static String defaultSessionHandler = "ch.eitchnet.privilege.handler.DefaultSessionHandler";
|
||||||
|
private static String defaultEncryptionHandler = "ch.eitchnet.privilege.handler.DefaultEncryptionHandler";
|
||||||
|
private static String defaultPolicyHandler = "ch.eitchnet.privilege.handler.DefaultPolicyHandler";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
BasicConfigurator.resetConfiguration();
|
||||||
|
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d %5p [%t] %C{1} %M - %m%n")));
|
||||||
|
Logger.getRootLogger().setLevel(Level.INFO);
|
||||||
|
|
||||||
|
// get current directory
|
||||||
|
path = System.getProperty("user.dir") + "/newConfig";
|
||||||
|
|
||||||
|
// ask user where to save configuration, default is pwd/newConfig/....
|
||||||
|
|
||||||
|
// see if path already exists
|
||||||
|
File pathF = new File(path);
|
||||||
|
if (pathF.exists()) {
|
||||||
|
throw new RuntimeException("Path already exists: " + pathF.getAbsolutePath());
|
||||||
|
} else {
|
||||||
|
if (!pathF.mkdirs()) {
|
||||||
|
throw new RuntimeException("Could not create path " + pathF.getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ask other questions...
|
||||||
|
|
||||||
|
// now perform work:
|
||||||
|
createXmlPrivilegeContainer();
|
||||||
|
createPolicyConfiguration();
|
||||||
|
createModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static void createModel() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static void createPolicyConfiguration() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static void createXmlPrivilegeContainer() {
|
||||||
|
|
||||||
|
// create document root
|
||||||
|
DocumentFactory factory = DocumentFactory.getInstance();
|
||||||
|
Document doc = factory.createDocument(XmlHelper.DEFAULT_ENCODING);
|
||||||
|
doc.setName(XmlConstants.XML_ROOT_PRIVILEGE_CONTAINER);
|
||||||
|
Element rootElement = factory.createElement(XmlConstants.XML_ROOT_PRIVILEGE_CONTAINER);
|
||||||
|
doc.setRootElement(rootElement);
|
||||||
|
|
||||||
|
Element parameterElement;
|
||||||
|
Element parametersElement;
|
||||||
|
|
||||||
|
// create PersistenceHandler
|
||||||
|
Element persistenceHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_PERSISTENCE);
|
||||||
|
rootElement.add(persistenceHandlerElem);
|
||||||
|
persistenceHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS, defaultPersistenceHandler);
|
||||||
|
parametersElement = factory.createElement(XmlConstants.XML_PARAMETERS);
|
||||||
|
persistenceHandlerElem.add(parametersElement);
|
||||||
|
// Parameter usersXmlFile
|
||||||
|
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
|
||||||
|
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_USERS_FILE);
|
||||||
|
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, usersFileName);
|
||||||
|
parametersElement.add(parameterElement);
|
||||||
|
// Parameter rolesXmlFile
|
||||||
|
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
|
||||||
|
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_ROLES_FILE);
|
||||||
|
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, rolesFileName);
|
||||||
|
parametersElement.add(parameterElement);
|
||||||
|
// Parameter privilegesXmlFile
|
||||||
|
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
|
||||||
|
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_PRIVILEGES_FILE);
|
||||||
|
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, privilegesFileName);
|
||||||
|
parametersElement.add(parameterElement);
|
||||||
|
|
||||||
|
// create SessionHandler
|
||||||
|
Element sessionHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_SESSION);
|
||||||
|
sessionHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS, defaultSessionHandler);
|
||||||
|
|
||||||
|
// create ModelHandler
|
||||||
|
Element modelHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_MODEL);
|
||||||
|
rootElement.add(modelHandlerElem);
|
||||||
|
modelHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS, "ch.eitchnet.privilege.handler.DefaultModelHandler");
|
||||||
|
|
||||||
|
// create EncryptionHandler
|
||||||
|
Element encryptionHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_ENCRYPTION);
|
||||||
|
rootElement.add(encryptionHandlerElem);
|
||||||
|
encryptionHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS, defaultEncryptionHandler);
|
||||||
|
parametersElement = factory.createElement(XmlConstants.XML_PARAMETERS);
|
||||||
|
encryptionHandlerElem.add(parametersElement);
|
||||||
|
// Parameter hashAlgorithm
|
||||||
|
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
|
||||||
|
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_HASH_ALGORITHM);
|
||||||
|
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, hashAlgorithm);
|
||||||
|
parametersElement.add(parameterElement);
|
||||||
|
|
||||||
|
// create PolicyHandler
|
||||||
|
Element policyHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_POLICY);
|
||||||
|
rootElement.add(policyHandlerElem);
|
||||||
|
policyHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS, defaultPolicyHandler);
|
||||||
|
parametersElement = factory.createElement(XmlConstants.XML_PARAMETERS);
|
||||||
|
policyHandlerElem.add(parametersElement);
|
||||||
|
// Parameter policyXmlFile
|
||||||
|
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
|
||||||
|
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_POLICY_FILE);
|
||||||
|
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, policyXmlFile);
|
||||||
|
parametersElement.add(parameterElement);
|
||||||
|
|
||||||
|
File privilegeContainerFile = new File(path + "/" + PrivilegeContainer.PRIVILEGE_CONTAINER_FILE);
|
||||||
|
XmlHelper.writeDocument(doc, privilegeContainerFile);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,79 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2010
|
|
||||||
*
|
|
||||||
* Robert von Burg
|
|
||||||
* eitch@eitchnet.ch
|
|
||||||
*
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package ch.eitchnet.privilege.helper;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
import org.apache.log4j.BasicConfigurator;
|
|
||||||
import org.apache.log4j.ConsoleAppender;
|
|
||||||
import org.apache.log4j.Level;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
import org.apache.log4j.PatternLayout;
|
|
||||||
|
|
||||||
import ch.eitchnet.privilege.base.PrivilegeContainer;
|
|
||||||
import ch.eitchnet.privilege.handler.ModelHandler;
|
|
||||||
import ch.eitchnet.privilege.model.Certificate;
|
|
||||||
import ch.eitchnet.privilege.model.UserRep;
|
|
||||||
import ch.eitchnet.privilege.model.UserState;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author rvonburg
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class TestConfigurationHelper {
|
|
||||||
private static final Logger logger = Logger.getLogger(TestConfigurationHelper.class);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param args
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
BasicConfigurator.resetConfiguration();
|
|
||||||
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d %5p [%t] %C{1} %M - %m%n")));
|
|
||||||
Logger.getRootLogger().setLevel(Level.INFO);
|
|
||||||
|
|
||||||
// initialize container
|
|
||||||
String pwd = System.getProperty("user.dir");
|
|
||||||
File privilegeContainerXml = new File(pwd + "/config/PrivilegeContainer.xml");
|
|
||||||
PrivilegeContainer privilegeContainer = PrivilegeContainer.getInstance();
|
|
||||||
privilegeContainer.initialize(privilegeContainerXml);
|
|
||||||
|
|
||||||
ModelHandler modelHandler = privilegeContainer.getModelHandler();
|
|
||||||
|
|
||||||
Certificate certificate = auth("eitch", "1234567890");
|
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++) {
|
|
||||||
// let's authenticate a session
|
|
||||||
auth("eitch", "1234567890");
|
|
||||||
}
|
|
||||||
|
|
||||||
// let's add a new user bob
|
|
||||||
UserRep userRep = new UserRep("bob", "Bob", "Newman", UserState.NEW, new HashSet<String>(), null);
|
|
||||||
modelHandler.addOrReplaceUser(certificate, userRep, null);
|
|
||||||
logger.info("Added user bob");
|
|
||||||
|
|
||||||
// TODO let's add a role
|
|
||||||
|
|
||||||
// TODO let's add a privilege
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static Certificate auth(String username, String password) {
|
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate(username, password);
|
|
||||||
logger.info("Auth took " + (System.currentTimeMillis() - start));
|
|
||||||
logger.info("Authenticated with certificate: " + certificate);
|
|
||||||
return certificate;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -35,6 +35,8 @@ import ch.eitchnet.privilege.i18n.PrivilegeException;
|
||||||
*/
|
*/
|
||||||
public class XmlHelper {
|
public class XmlHelper {
|
||||||
|
|
||||||
|
public static final String DEFAULT_ENCODING = "UTF-8";
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(XmlHelper.class);
|
private static final Logger logger = Logger.getLogger(XmlHelper.class);
|
||||||
|
|
||||||
public static Document parseDocument(File xmlFile) {
|
public static Document parseDocument(File xmlFile) {
|
||||||
|
@ -56,19 +58,20 @@ public class XmlHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeDocument(Element rootElement, File file) {
|
public static void writeDocument(Document document, File file) {
|
||||||
|
|
||||||
logger.info("Exporting root element " + rootElement.getName() + " to " + file.getAbsolutePath());
|
logger.info("Exporting document element " + document.getName() + " to " + file.getAbsolutePath());
|
||||||
|
|
||||||
OutputStream fileOutputStream = null;
|
OutputStream fileOutputStream = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Document document = DocumentFactory.getInstance().createDocument();
|
|
||||||
document.setRootElement(rootElement);
|
|
||||||
|
|
||||||
fileOutputStream = new FileOutputStream(file);
|
fileOutputStream = new FileOutputStream(file);
|
||||||
|
|
||||||
String aEncodingScheme = "UTF-8";
|
String aEncodingScheme = document.getXMLEncoding();
|
||||||
|
if (aEncodingScheme == null || aEncodingScheme.isEmpty()) {
|
||||||
|
aEncodingScheme = DEFAULT_ENCODING;
|
||||||
|
}
|
||||||
OutputFormat outformat = OutputFormat.createPrettyPrint();
|
OutputFormat outformat = OutputFormat.createPrettyPrint();
|
||||||
outformat.setEncoding(aEncodingScheme);
|
outformat.setEncoding(aEncodingScheme);
|
||||||
XMLWriter writer = new XMLWriter(fileOutputStream, outformat);
|
XMLWriter writer = new XMLWriter(fileOutputStream, outformat);
|
||||||
|
@ -90,4 +93,13 @@ public class XmlHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void writeDocument(Element rootElement, File file) {
|
||||||
|
|
||||||
|
Document document = DocumentFactory.getInstance().createDocument(DEFAULT_ENCODING);
|
||||||
|
document.setRootElement(rootElement);
|
||||||
|
document.setName(rootElement.getName());
|
||||||
|
|
||||||
|
writeDocument(document, file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
package ch.eitchnet.privilege.model.internal;
|
package ch.eitchnet.privilege.model.internal;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -56,7 +57,7 @@ public final class User {
|
||||||
this.firstname = firstname;
|
this.firstname = firstname;
|
||||||
this.surname = surname;
|
this.surname = surname;
|
||||||
|
|
||||||
this.roles = roles;
|
this.roles = Collections.unmodifiableSet(roles);
|
||||||
|
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ import org.apache.log4j.ConsoleAppender;
|
||||||
import org.apache.log4j.Level;
|
import org.apache.log4j.Level;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.apache.log4j.PatternLayout;
|
import org.apache.log4j.PatternLayout;
|
||||||
import org.junit.Before;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import ch.eitchnet.privilege.base.PrivilegeContainer;
|
import ch.eitchnet.privilege.base.PrivilegeContainer;
|
||||||
|
@ -40,8 +40,8 @@ public class PrivilegeTest {
|
||||||
/**
|
/**
|
||||||
* @throws java.lang.Exception
|
* @throws java.lang.Exception
|
||||||
*/
|
*/
|
||||||
@Before
|
@BeforeClass
|
||||||
public void setUp() throws Exception {
|
public static void init() throws Exception {
|
||||||
|
|
||||||
// set up log4j
|
// set up log4j
|
||||||
BasicConfigurator.resetConfiguration();
|
BasicConfigurator.resetConfiguration();
|
||||||
|
@ -64,14 +64,14 @@ public class PrivilegeTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = AccessDeniedException.class)
|
@Test(expected = AccessDeniedException.class)
|
||||||
public void testAuthenticationNOk() throws Exception {
|
public void testFailAuthenticationNOk() throws Exception {
|
||||||
|
|
||||||
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate("eitch", "123");
|
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate("eitch", "123");
|
||||||
org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
|
org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = PrivilegeException.class)
|
@Test(expected = PrivilegeException.class)
|
||||||
public void testAuthenticationPWNull() throws Exception {
|
public void testFailAuthenticationPWNull() throws Exception {
|
||||||
|
|
||||||
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate("eitch", null);
|
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate("eitch", null);
|
||||||
org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
|
org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
|
||||||
|
@ -101,7 +101,7 @@ public class PrivilegeTest {
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Test(expected = AccessDeniedException.class)
|
@Test(expected = AccessDeniedException.class)
|
||||||
public void testAuthAsBob() throws Exception {
|
public void testFailAuthAsBob() throws Exception {
|
||||||
|
|
||||||
PrivilegeContainer.getInstance().getSessionHandler().authenticate("bob", "12345678901");
|
PrivilegeContainer.getInstance().getSessionHandler().authenticate("bob", "12345678901");
|
||||||
}
|
}
|
||||||
|
@ -116,13 +116,42 @@ public class PrivilegeTest {
|
||||||
modelHandler.setUserState(certificate, "bob", UserState.ENABLED);
|
modelHandler.setUserState(certificate, "bob", UserState.ENABLED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will fail as user bob has no role
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@Test(expected = PrivilegeException.class)
|
||||||
|
public void testFailAuthUserBob() throws Exception {
|
||||||
|
|
||||||
|
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate("bob",
|
||||||
|
"12345678901");
|
||||||
|
org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddUserRoleToBob() throws Exception {
|
||||||
|
|
||||||
|
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate("eitch",
|
||||||
|
"1234567890");
|
||||||
|
|
||||||
|
ModelHandler modelHandler = PrivilegeContainer.getInstance().getModelHandler();
|
||||||
|
modelHandler.addRoleToUser(certificate, "bob", "user");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAuthAsBob() throws Exception {
|
||||||
|
|
||||||
|
PrivilegeContainer.getInstance().getSessionHandler().authenticate("bob", "12345678901");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will fail because user bob does not have admin rights
|
* Will fail because user bob does not have admin rights
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Test(expected = AccessDeniedException.class)
|
@Test(expected = AccessDeniedException.class)
|
||||||
public void testAddUserTedAsBob() throws Exception {
|
public void testFailAddUserTedAsBob() throws Exception {
|
||||||
|
|
||||||
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate("bob",
|
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate("bob",
|
||||||
"12345678901");
|
"12345678901");
|
||||||
|
@ -133,4 +162,27 @@ public class PrivilegeTest {
|
||||||
PrivilegeContainer.getInstance().getModelHandler().addOrReplaceUser(certificate, userRep, null);
|
PrivilegeContainer.getInstance().getModelHandler().addOrReplaceUser(certificate, userRep, null);
|
||||||
logger.info("Added user bob");
|
logger.info("Added user bob");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddAdminRoleToBob() throws Exception {
|
||||||
|
|
||||||
|
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate("eitch",
|
||||||
|
"1234567890");
|
||||||
|
|
||||||
|
ModelHandler modelHandler = PrivilegeContainer.getInstance().getModelHandler();
|
||||||
|
modelHandler.addRoleToUser(certificate, "bob", PrivilegeContainer.PRIVILEGE_ADMIN_ROLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddUserTedAsBob() throws Exception {
|
||||||
|
|
||||||
|
Certificate certificate = PrivilegeContainer.getInstance().getSessionHandler().authenticate("bob",
|
||||||
|
"12345678901");
|
||||||
|
org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
|
||||||
|
|
||||||
|
// let's add a new user ted
|
||||||
|
UserRep userRep = new UserRep("ted", "Ted", "Newman", UserState.NEW, new HashSet<String>(), null);
|
||||||
|
PrivilegeContainer.getInstance().getModelHandler().addOrReplaceUser(certificate, userRep, null);
|
||||||
|
logger.info("Added user bob");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue