Search in sources :

Example 11 with User

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;
}
Also used : LDAPEntry(com.novell.ldap.LDAPEntry) LDAPSearchResults(com.novell.ldap.LDAPSearchResults) User(fi.otavanopisto.pyramus.domainmodel.users.User) UserDAO(fi.otavanopisto.pyramus.dao.users.UserDAO) LDAPException(com.novell.ldap.LDAPException) AuthenticationException(fi.otavanopisto.pyramus.plugin.auth.AuthenticationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) LDAPConnection(com.novell.ldap.LDAPConnection)

Example 12 with User

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");
}
Also used : Role(fi.otavanopisto.pyramus.domainmodel.users.Role) UserRole(fi.otavanopisto.pyramus.framework.UserRole) User(fi.otavanopisto.pyramus.domainmodel.users.User) UserDAO(fi.otavanopisto.pyramus.dao.users.UserDAO) ArrayList(java.util.ArrayList) EmailDAO(fi.otavanopisto.pyramus.dao.base.EmailDAO)

Example 13 with User

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);
    }
}
Also used : StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) User(fi.otavanopisto.pyramus.domainmodel.users.User) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) StaffMember(fi.otavanopisto.pyramus.domainmodel.users.StaffMember)

Example 14 with User

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());
        }
    }
}
Also used : User(fi.otavanopisto.pyramus.domainmodel.users.User) HashMap(java.util.HashMap) Attribute(javax.persistence.metamodel.Attribute) ChangeLogEntryEntity(fi.otavanopisto.pyramus.domainmodel.changelog.ChangeLogEntryEntity) ChangeLogEntryPropertyDAO(fi.otavanopisto.pyramus.dao.changelog.ChangeLogEntryPropertyDAO) ChangeLogEntry(fi.otavanopisto.pyramus.domainmodel.changelog.ChangeLogEntry) Date(java.util.Date) ChangeLogEntryEntityProperty(fi.otavanopisto.pyramus.domainmodel.changelog.ChangeLogEntryEntityProperty) ChangeLogEntryEntityPropertyDAO(fi.otavanopisto.pyramus.dao.changelog.ChangeLogEntryEntityPropertyDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) ChangeLogEntryProperty(fi.otavanopisto.pyramus.domainmodel.changelog.ChangeLogEntryProperty) ChangeLogEntryEntityDAO(fi.otavanopisto.pyramus.dao.changelog.ChangeLogEntryEntityDAO) SystemDAO(fi.otavanopisto.pyramus.dao.SystemDAO) ChangeLogEntryDAO(fi.otavanopisto.pyramus.dao.changelog.ChangeLogEntryDAO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with User

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);
        }
    }
}
Also used : User(fi.otavanopisto.pyramus.domainmodel.users.User) Attribute(javax.persistence.metamodel.Attribute) ChangeLogEntryEntity(fi.otavanopisto.pyramus.domainmodel.changelog.ChangeLogEntryEntity) ChangeLogEntryPropertyDAO(fi.otavanopisto.pyramus.dao.changelog.ChangeLogEntryPropertyDAO) ChangeLogEntry(fi.otavanopisto.pyramus.domainmodel.changelog.ChangeLogEntry) Date(java.util.Date) ChangeLogEntryEntityProperty(fi.otavanopisto.pyramus.domainmodel.changelog.ChangeLogEntryEntityProperty) ChangeLogEntryEntityPropertyDAO(fi.otavanopisto.pyramus.dao.changelog.ChangeLogEntryEntityPropertyDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) ChangeLogEntryEntityDAO(fi.otavanopisto.pyramus.dao.changelog.ChangeLogEntryEntityDAO) SystemDAO(fi.otavanopisto.pyramus.dao.SystemDAO) ChangeLogEntryDAO(fi.otavanopisto.pyramus.dao.changelog.ChangeLogEntryDAO)

Aggregations

User (fi.otavanopisto.pyramus.domainmodel.users.User)107 StaffMemberDAO (fi.otavanopisto.pyramus.dao.users.StaffMemberDAO)43 Student (fi.otavanopisto.pyramus.domainmodel.students.Student)29 RESTPermit (fi.otavanopisto.pyramus.rest.annotation.RESTPermit)28 Path (javax.ws.rs.Path)28 StaffMember (fi.otavanopisto.pyramus.domainmodel.users.StaffMember)24 Date (java.util.Date)23 UserDAO (fi.otavanopisto.pyramus.dao.users.UserDAO)22 Person (fi.otavanopisto.pyramus.domainmodel.base.Person)21 HashSet (java.util.HashSet)18 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)15 StudentGroup (fi.otavanopisto.pyramus.domainmodel.students.StudentGroup)15 StudentDAO (fi.otavanopisto.pyramus.dao.students.StudentDAO)14 EducationalTimeUnit (fi.otavanopisto.pyramus.domainmodel.base.EducationalTimeUnit)14 Tag (fi.otavanopisto.pyramus.domainmodel.base.Tag)14 PersonDAO (fi.otavanopisto.pyramus.dao.base.PersonDAO)13 Organization (fi.otavanopisto.pyramus.domainmodel.base.Organization)12 StudentGroupUser (fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser)12 GET (javax.ws.rs.GET)12 DefaultsDAO (fi.otavanopisto.pyramus.dao.base.DefaultsDAO)11