Search in sources :

Example 6 with NoSuchPrincipalException

use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.

the class WikiContext method requiredPermission.

/**
 * Returns the permission required to successfully execute this context.
 * For example, the a wiki context of VIEW for a certain page means that
 * the PagePermission "view" is required for the page. In some cases, no
 * particular permission is required, in which case a dummy permission will
 * be returned ({@link java.util.PropertyPermission}<code> "os.name",
 * "read"</code>). This method is guaranteed to always return a valid,
 * non-null permission.
 * @return the permission
 * @since 2.4
 */
public Permission requiredPermission() {
    // This is a filthy rotten hack -- absolutely putrid
    if (WikiCommand.INSTALL.equals(m_command)) {
        // See if admin users exists
        boolean adminExists = false;
        try {
            UserManager userMgr = m_engine.getUserManager();
            UserDatabase userDb = userMgr.getUserDatabase();
            userDb.findByLoginName(Installer.ADMIN_ID);
            adminExists = true;
        } catch (NoSuchPrincipalException e) {
            return DUMMY_PERMISSION;
        }
        if (adminExists) {
            return new AllPermission(m_engine.getApplicationName());
        }
    }
    // method returns null, but until then we will use this hack
    if (m_command.requiredPermission() == null) {
        return DUMMY_PERMISSION;
    }
    return m_command.requiredPermission();
}
Also used : UserManager(org.apache.wiki.auth.UserManager) UserDatabase(org.apache.wiki.auth.user.UserDatabase) AllPermission(org.apache.wiki.auth.permissions.AllPermission) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException)

Example 7 with NoSuchPrincipalException

use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.

the class AbstractUserDatabase method getPrincipals.

/**
 * <p>Looks up the Principals representing a user from the user database. These
 * are defined as a set of WikiPrincipals manufactured from the login name,
 * full name, and wiki name. If the user database does not contain a user
 * with the supplied identifier, throws a {@link NoSuchPrincipalException}.</p>
 * <p>When this method creates WikiPrincipals, the Principal containing
 * the user's full name is marked as containing the common name (see
 * {@link org.apache.wiki.auth.WikiPrincipal#WikiPrincipal(String, String)}).
 * @param identifier the name of the principal to retrieve; this corresponds to
 *            value returned by the user profile's
 *            {@link UserProfile#getLoginName()}method.
 * @return the array of Principals representing the user
 * @see org.apache.wiki.auth.user.UserDatabase#getPrincipals(java.lang.String)
 * @throws NoSuchPrincipalException {@inheritDoc}
 */
public Principal[] getPrincipals(String identifier) throws NoSuchPrincipalException {
    try {
        UserProfile profile = findByLoginName(identifier);
        ArrayList<Principal> principals = new ArrayList<Principal>();
        if (profile.getLoginName() != null && profile.getLoginName().length() > 0) {
            principals.add(new WikiPrincipal(profile.getLoginName(), WikiPrincipal.LOGIN_NAME));
        }
        if (profile.getFullname() != null && profile.getFullname().length() > 0) {
            principals.add(new WikiPrincipal(profile.getFullname(), WikiPrincipal.FULL_NAME));
        }
        if (profile.getWikiName() != null && profile.getWikiName().length() > 0) {
            principals.add(new WikiPrincipal(profile.getWikiName(), WikiPrincipal.WIKI_NAME));
        }
        return principals.toArray(new Principal[principals.size()]);
    } catch (NoSuchPrincipalException e) {
        throw e;
    }
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) ArrayList(java.util.ArrayList) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

Example 8 with NoSuchPrincipalException

use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.

the class JDBCUserDatabase method deleteByLoginName.

/**
 * Looks up and deletes the first {@link UserProfile} in the user database
 * that matches a profile having a given login name. If the user database
 * does not contain a user with a matching attribute, throws a
 * {@link NoSuchPrincipalException}. This method is intended to be atomic;
 * results cannot be partially committed. If the commit fails, it should
 * roll back its state appropriately. Implementing classes that persist to
 * the file system may wish to make this method <code>synchronized</code>.
 *
 * @param loginName the login name of the user profile that shall be deleted
 */
public void deleteByLoginName(String loginName) throws NoSuchPrincipalException, WikiSecurityException {
    // Get the existing user; if not found, throws NoSuchPrincipalException
    findByLoginName(loginName);
    Connection conn = null;
    try {
        // Open the database connection
        conn = m_ds.getConnection();
        if (m_supportsCommits) {
            conn.setAutoCommit(false);
        }
        PreparedStatement ps;
        // Delete user record
        ps = conn.prepareStatement(m_deleteUserByLoginName);
        ps.setString(1, loginName);
        ps.execute();
        ps.close();
        // Delete role record
        ps = conn.prepareStatement(m_deleteRoleByLoginName);
        ps.setString(1, loginName);
        ps.execute();
        ps.close();
        // Commit and close connection
        if (m_supportsCommits) {
            conn.commit();
        }
    } catch (SQLException e) {
        throw new WikiSecurityException(e.getMessage(), e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) NoRequiredPropertyException(org.apache.wiki.api.exceptions.NoRequiredPropertyException) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) IOException(java.io.IOException)

Example 9 with NoSuchPrincipalException

use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.

the class XMLUserDatabase method deleteByLoginName.

/**
 * Looks up and deletes the first {@link UserProfile} in the user database
 * that matches a profile having a given login name. If the user database
 * does not contain a user with a matching attribute, throws a
 * {@link NoSuchPrincipalException}.
 * @param loginName the login name of the user profile that shall be deleted
 */
public synchronized void deleteByLoginName(String loginName) throws NoSuchPrincipalException, WikiSecurityException {
    if (c_dom == null) {
        throw new WikiSecurityException("FATAL: database does not exist");
    }
    NodeList users = c_dom.getDocumentElement().getElementsByTagName(USER_TAG);
    for (int i = 0; i < users.getLength(); i++) {
        Element user = (Element) users.item(i);
        if (user.getAttribute(LOGIN_NAME).equals(loginName)) {
            c_dom.getDocumentElement().removeChild(user);
            // Commit to disk
            saveDOM();
            return;
        }
    }
    throw new NoSuchPrincipalException("Not in database: " + loginName);
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException)

Example 10 with NoSuchPrincipalException

use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.

the class JDBCUserDatabase method save.

/**
 * @see org.apache.wiki.auth.user.UserDatabase#save(org.apache.wiki.auth.user.UserProfile)
 */
public void save(UserProfile profile) throws WikiSecurityException {
    String initialRole = "Authenticated";
    // Figure out which prepared statement to use & execute it
    String loginName = profile.getLoginName();
    PreparedStatement ps = null;
    UserProfile existingProfile = null;
    try {
        existingProfile = findByLoginName(loginName);
    } catch (NoSuchPrincipalException e) {
    // Existing profile will be null
    }
    // Get a clean password from the passed profile.
    // Blank password is the same as null, which means we re-use the
    // existing one.
    String password = profile.getPassword();
    String existingPassword = (existingProfile == null) ? null : existingProfile.getPassword();
    if (NOTHING.equals(password)) {
        password = null;
    }
    if (password == null) {
        password = existingPassword;
    }
    // If password changed, hash it before we save
    if (!StringUtils.equals(password, existingPassword)) {
        password = getHash(password);
    }
    Connection conn = null;
    try {
        // Open the database connection
        conn = m_ds.getConnection();
        if (m_supportsCommits) {
            conn.setAutoCommit(false);
        }
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        Date modDate = new Date(ts.getTime());
        java.sql.Date lockExpiry = profile.getLockExpiry() == null ? null : new java.sql.Date(profile.getLockExpiry().getTime());
        if (existingProfile == null) {
            // User is new: insert new user record
            ps = conn.prepareStatement(m_insertProfile);
            ps.setString(1, profile.getUid());
            ps.setString(2, profile.getEmail());
            ps.setString(3, profile.getFullname());
            ps.setString(4, password);
            ps.setString(5, profile.getWikiName());
            ps.setTimestamp(6, ts);
            ps.setString(7, profile.getLoginName());
            try {
                ps.setString(8, Serializer.serializeToBase64(profile.getAttributes()));
            } catch (IOException e) {
                throw new WikiSecurityException("Could not save user profile attribute. Reason: " + e.getMessage(), e);
            }
            ps.setTimestamp(9, ts);
            ps.execute();
            ps.close();
            // Insert new role record
            ps = conn.prepareStatement(m_findRoles);
            ps.setString(1, profile.getLoginName());
            ResultSet rs = ps.executeQuery();
            int roles = 0;
            while (rs.next()) {
                roles++;
            }
            ps.close();
            if (roles == 0) {
                ps = conn.prepareStatement(m_insertRole);
                ps.setString(1, profile.getLoginName());
                ps.setString(2, initialRole);
                ps.execute();
                ps.close();
            }
            // Set the profile creation time
            profile.setCreated(modDate);
        } else {
            // User exists: modify existing record
            ps = conn.prepareStatement(m_updateProfile);
            ps.setString(1, profile.getUid());
            ps.setString(2, profile.getEmail());
            ps.setString(3, profile.getFullname());
            ps.setString(4, password);
            ps.setString(5, profile.getWikiName());
            ps.setTimestamp(6, ts);
            ps.setString(7, profile.getLoginName());
            try {
                ps.setString(8, Serializer.serializeToBase64(profile.getAttributes()));
            } catch (IOException e) {
                throw new WikiSecurityException("Could not save user profile attribute. Reason: " + e.getMessage(), e);
            }
            ps.setDate(9, lockExpiry);
            ps.setString(10, profile.getLoginName());
            ps.execute();
            ps.close();
        }
        // Set the profile mod time
        profile.setLastModified(modDate);
        // Commit and close connection
        if (m_supportsCommits) {
            conn.commit();
        }
    } catch (SQLException e) {
        throw new WikiSecurityException(e.getMessage(), e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) IOException(java.io.IOException) Timestamp(java.sql.Timestamp) Date(java.util.Date) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) NoRequiredPropertyException(org.apache.wiki.api.exceptions.NoRequiredPropertyException) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) IOException(java.io.IOException) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) ResultSet(java.sql.ResultSet)

Aggregations

NoSuchPrincipalException (org.apache.wiki.auth.NoSuchPrincipalException)16 WikiSecurityException (org.apache.wiki.auth.WikiSecurityException)9 IOException (java.io.IOException)5 Date (java.util.Date)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 NamingException (javax.naming.NamingException)4 NoRequiredPropertyException (org.apache.wiki.api.exceptions.NoRequiredPropertyException)4 UserManager (org.apache.wiki.auth.UserManager)4 WikiPrincipal (org.apache.wiki.auth.WikiPrincipal)4 UserDatabase (org.apache.wiki.auth.user.UserDatabase)4 UserProfile (org.apache.wiki.auth.user.UserProfile)3 Principal (java.security.Principal)2 ResultSet (java.sql.ResultSet)2 Timestamp (java.sql.Timestamp)2 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)2 Element (org.w3c.dom.Element)2 NodeList (org.w3c.dom.NodeList)2 DateFormat (java.text.DateFormat)1