use of fi.otavanopisto.pyramus.domainmodel.users.User in project pyramus by otavanopisto.
the class LDAPAuthorizationStrategy method getUser.
/**
* Returns the user corresponding to the given credentials. If no user cannot be found, returns
* <code>null</code>.
*
* @param username The username
* @param password The password
*
* @return The user corresponding to the given credentials, or <code>null</code> if not found
* @throws AuthenticationException
*/
public User getUser(String username, String password) throws AuthenticationException {
UserDAO userDAO = DAOFactory.getInstance().getUserDAO();
LDAPConnection connection;
try {
connection = LDAPUtils.getLDAPConnection();
final String searchFilter = "(" + System.getProperty("authentication.ldap.usernameAttr") + "=" + username + ")";
final LDAPSearchResults searchResults = connection.search(System.getProperty("authentication.ldap.authdn"), LDAPConnection.SCOPE_SUB, searchFilter, null, false);
if (searchResults != null && searchResults.hasMore()) {
LDAPEntry entry = searchResults.next();
try {
String uniqueIdAttr = System.getProperty("authentication.ldap.uniqueIdAttr");
boolean idEncoded = "1".equals(System.getProperty("authentication.ldap.uniqueIdEncoded"));
connection.bind(Integer.parseInt(System.getProperty("authentication.ldap.version")), entry.getDN(), password.getBytes("UTF8"));
String id = idEncoded ? LDAPUtils.getAttributeBinaryValue(entry.getAttribute(uniqueIdAttr)) : entry.getAttribute(uniqueIdAttr).getStringValue();
User user = userDAO.findByExternalIdAndAuthProvider(id, getName());
if (user == null)
throw new AuthenticationException(AuthenticationException.LOCAL_USER_MISSING);
return user;
} catch (UnsupportedEncodingException e) {
throw new LDAPException();
}
}
} catch (LDAPException e) {
throw new SmvcRuntimeException(e);
}
return null;
}
use of fi.otavanopisto.pyramus.domainmodel.users.User in project pyramus by otavanopisto.
the class ImportLDAPUsersViewController method processSend.
public void processSend(PageRequestContext requestContext) {
EmailDAO emailDAO = DAOFactory.getInstance().getEmailDAO();
UserDAO userDAO = DAOFactory.getInstance().getUserDAO();
List<User> createdUsers = new ArrayList<User>();
int rowCount = requestContext.getInteger("importTable.rowCount");
for (int i = 0; i < rowCount; i++) {
String colPrefix = "importTable." + i;
if ("1".equals(requestContext.getString(colPrefix + ".import"))) {
String email = requestContext.getString(colPrefix + ".email");
String firstName = requestContext.getString(colPrefix + ".firstName");
String lastName = requestContext.getString(colPrefix + ".lastName");
String roleName = requestContext.getString(colPrefix + ".role");
String id = requestContext.getString(colPrefix + ".id");
Role role = Enum.valueOf(Role.class, roleName);
User user = userDAO.create(firstName, lastName, id, "LDAP", role);
emailDAO.create(user.getContactInfo(), null, Boolean.TRUE, email);
createdUsers.add(user);
}
}
requestContext.getRequest().setAttribute("createdUsers", createdUsers);
requestContext.setRedirectURL(requestContext.getRequest().getContextPath() + "system/importldapusers.page");
}
use of fi.otavanopisto.pyramus.domainmodel.users.User in project pyramus by otavanopisto.
the class UsersAutoCompleteBinaryRequestController method process.
/**
* Processes a binary request.
* The request should contain the following parameters:
* <dl>
* <dt><code>text</code></dt>
* <dd>Already typed characters.</dd>
* </dl>
*
* @param binaryRequestContext The context of the binary request.
*/
public void process(BinaryRequestContext binaryRequestContext) {
StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
String text = binaryRequestContext.getString("text");
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.append("<ul>");
if (!StringUtils.isBlank(text)) {
text = QueryParser.escape(StringUtils.trim(text)) + '*';
List<StaffMember> users = userDAO.searchUsersBasic(100, 0, text).getResults();
for (User user : users) {
addUser(resultBuilder, user);
}
}
resultBuilder.append("</ul>");
try {
binaryRequestContext.setResponseContent(resultBuilder.toString().getBytes("UTF-8"), "text/html;charset=UTF-8");
} catch (UnsupportedEncodingException e) {
throw new SmvcRuntimeException(e);
}
}
use of fi.otavanopisto.pyramus.domainmodel.users.User in project pyramus by otavanopisto.
the class ChangeLogJPAEventsListener method handleUpdate.
private void handleUpdate(MapMessage mapMessage) throws JMSException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
SystemDAO systemDAO = DAOFactory.getInstance().getSystemDAO();
StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
ChangeLogEntryDAO logEntryDAO = DAOFactory.getInstance().getChangeLogEntryDAO();
ChangeLogEntryEntityDAO entryEntityDAO = DAOFactory.getInstance().getChangeLogEntryEntityDAO();
ChangeLogEntryEntityPropertyDAO entryEntityPropertyDAO = DAOFactory.getInstance().getChangeLogEntryEntityPropertyDAO();
ChangeLogEntryPropertyDAO logEntryPropertyDAO = DAOFactory.getInstance().getChangeLogEntryPropertyDAO();
Long loggedUserId = mapMessage.itemExists("loggedUserId") ? mapMessage.getLong("loggedUserId") : null;
User loggedUser = loggedUserId != null ? userDAO.findById(loggedUserId) : null;
String entityClassName = mapMessage.getString("entity");
Object id = mapMessage.getObject("id");
Date time = new Date(mapMessage.getLong("time"));
Class<?> entityClass = Class.forName(entityClassName);
Object entity = systemDAO.findEntityById(entityClass, id);
Map<ChangeLogEntryEntityProperty, String> values = new HashMap<>();
// First we need to check if ChangeLogEntryEntity is already in the database
ChangeLogEntryEntity changeLogEntryEntity = entryEntityDAO.findByName(entityClassName);
if (changeLogEntryEntity == null) {
// if not we need to add it
changeLogEntryEntity = entryEntityDAO.create(entityClassName);
}
// After that we add all changed properties into the values map
Set<Attribute<?, ?>> attributes = systemDAO.getEntityAttributes(entityClass);
for (Attribute<?, ?> attribute : attributes) {
String fieldName = attribute.getName();
if (TrackedEntityUtils.isTrackedProperty(entityClassName, fieldName)) {
ChangeLogEntryEntityProperty changeLogEntryEntityProperty = entryEntityPropertyDAO.findByEntityAndName(changeLogEntryEntity, fieldName);
if (changeLogEntryEntityProperty == null) {
changeLogEntryEntityProperty = entryEntityPropertyDAO.create(changeLogEntryEntity, fieldName);
}
String newValue = null;
switch(attribute.getPersistentAttributeType()) {
case BASIC:
newValue = String.valueOf(ReflectionApiUtils.getObjectFieldValue(entity, fieldName, true));
break;
case ONE_TO_ONE:
case MANY_TO_ONE:
Object joinedEntity = ReflectionApiUtils.getObjectFieldValue(entity, fieldName, true);
if (joinedEntity != null) {
newValue = String.valueOf(getEntityId(attribute.getJavaType(), joinedEntity));
}
break;
}
ChangeLogEntryProperty changeLogEntryProperty = logEntryPropertyDAO.findLatestByEntryEntityProperty(changeLogEntryEntityProperty, String.valueOf(id));
String oldValue = changeLogEntryProperty != null ? changeLogEntryProperty.getValue() : null;
if (newValue == null ? oldValue != null ? true : false : !newValue.equals(oldValue)) {
values.put(changeLogEntryEntityProperty, newValue);
}
}
}
// And finally we can iterate values map values into the database
if (!values.isEmpty()) {
ChangeLogEntry entry = logEntryDAO.create(changeLogEntryEntity, ChangeLogEntryType.Update, String.valueOf(id), time, loggedUser);
for (Map.Entry<ChangeLogEntryEntityProperty, String> changeLogEntry : values.entrySet()) {
logEntryPropertyDAO.create(entry, changeLogEntry.getKey(), changeLogEntry.getValue());
}
}
}
use of fi.otavanopisto.pyramus.domainmodel.users.User in project pyramus by otavanopisto.
the class ChangeLogJPAEventsListener method handleCreate.
private void handleCreate(MapMessage mapMessage) throws JMSException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
SystemDAO systemDAO = DAOFactory.getInstance().getSystemDAO();
StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
ChangeLogEntryDAO logEntryDAO = DAOFactory.getInstance().getChangeLogEntryDAO();
ChangeLogEntryEntityDAO entryEntityDAO = DAOFactory.getInstance().getChangeLogEntryEntityDAO();
ChangeLogEntryEntityPropertyDAO entryEntityPropertyDAO = DAOFactory.getInstance().getChangeLogEntryEntityPropertyDAO();
ChangeLogEntryPropertyDAO logEntryPropertyDAO = DAOFactory.getInstance().getChangeLogEntryPropertyDAO();
Long loggedUserId = mapMessage.itemExists("loggedUserId") ? mapMessage.getLong("loggedUserId") : null;
User loggedUser = loggedUserId != null ? userDAO.findById(loggedUserId) : null;
String entityClassName = mapMessage.getString("entity");
Object id = mapMessage.getObject("id");
Date time = new Date(mapMessage.getLong("time"));
Class<?> entityClass = Class.forName(entityClassName);
Object entity = systemDAO.findEntityById(entityClass, id);
// First we need to check if ChangeLogEntryEntity is already in the database
ChangeLogEntryEntity changeLogEntryEntity = entryEntityDAO.findByName(entityClassName);
if (changeLogEntryEntity == null) {
// if not we need to add it
changeLogEntryEntity = entryEntityDAO.create(entityClassName);
}
// Then we can add the log entry
ChangeLogEntry entry = logEntryDAO.create(changeLogEntryEntity, ChangeLogEntryType.Create, String.valueOf(id), time, loggedUser);
// After the entry has been added we can add all initial properties into the entry
Set<Attribute<?, ?>> attributes = systemDAO.getEntityAttributes(entityClass);
for (Attribute<?, ?> attribute : attributes) {
String fieldName = attribute.getName();
if (TrackedEntityUtils.isTrackedProperty(entityClassName, fieldName)) {
String value = null;
switch(attribute.getPersistentAttributeType()) {
case BASIC:
value = String.valueOf(ReflectionApiUtils.getObjectFieldValue(entity, fieldName, true));
break;
case ONE_TO_ONE:
case MANY_TO_ONE:
Object joinedEntity = ReflectionApiUtils.getObjectFieldValue(entity, fieldName, true);
if (joinedEntity != null) {
value = String.valueOf(getEntityId(attribute.getJavaType(), joinedEntity));
}
break;
}
// We need to check if database already contains this entity property
ChangeLogEntryEntityProperty changeLogEntryEntityProperty = entryEntityPropertyDAO.findByEntityAndName(changeLogEntryEntity, fieldName);
if (changeLogEntryEntityProperty == null) {
// if not we add it there
changeLogEntryEntityProperty = entryEntityPropertyDAO.create(changeLogEntryEntity, fieldName);
}
// After entity property has been resolved we can add the property itself
logEntryPropertyDAO.create(entry, changeLogEntryEntityProperty, value);
}
}
}
Aggregations