Search in sources :

Example 1 with WikiSecurityException

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

the class MetaWeblogHandler method checkPermissions.

/**
 *  Does a quick check against the current user
 *  and does he have permissions to do the stuff
 *  that he really wants to.
 *  <p>
 *  If there is no authentication enabled, returns normally.
 *
 *  @throw XmlRpcException with the correct error message, if auth fails.
 */
private void checkPermissions(WikiPage page, String username, String password, String permission) throws XmlRpcException {
    try {
        AuthenticationManager amm = m_context.getEngine().getAuthenticationManager();
        AuthorizationManager mgr = m_context.getEngine().getAuthorizationManager();
        if (amm.login(m_context.getWikiSession(), m_context.getHttpRequest(), username, password)) {
            if (!mgr.checkPermission(m_context.getWikiSession(), PermissionFactory.getPagePermission(page, permission))) {
                throw new XmlRpcException(1, "No permission");
            }
        } else {
            throw new XmlRpcException(1, "Unknown login");
        }
    } catch (WikiSecurityException e) {
        throw new XmlRpcException(1, e.getMessage(), e);
    }
    return;
}
Also used : AuthenticationManager(org.apache.wiki.auth.AuthenticationManager) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 2 with WikiSecurityException

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

the class Installer method createAdministrator.

/**
 * Creates an administrative user and returns the new password.
 * If the admin user exists, the password will be <code>null</code>.
 * @return the password
 * @throws WikiSecurityException
 */
public String createAdministrator() throws WikiSecurityException {
    if (!m_validated) {
        throw new WikiSecurityException("Cannot create administrator because one or more of the installation settings are invalid.");
    }
    if (adminExists()) {
        return null;
    }
    // See if the admin user exists already
    UserManager userMgr = m_engine.getUserManager();
    UserDatabase userDb = userMgr.getUserDatabase();
    String password = null;
    try {
        userDb.findByLoginName(ADMIN_ID);
    } catch (NoSuchPrincipalException e) {
        // Create a random 12-character password
        password = TextUtil.generateRandomPassword();
        UserProfile profile = userDb.newProfile();
        profile.setLoginName(ADMIN_ID);
        profile.setFullname(ADMIN_NAME);
        profile.setPassword(password);
        userDb.save(profile);
    }
    // Create a new admin group
    GroupManager groupMgr = m_engine.getGroupManager();
    Group group = null;
    try {
        group = groupMgr.getGroup(ADMIN_GROUP);
        group.add(new WikiPrincipal(ADMIN_NAME));
    } catch (NoSuchPrincipalException e) {
        group = groupMgr.parseGroup(ADMIN_GROUP, ADMIN_NAME, true);
    }
    groupMgr.setGroup(m_session, group);
    return password;
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) Group(org.apache.wiki.auth.authorize.Group) UserProfile(org.apache.wiki.auth.user.UserProfile) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) UserManager(org.apache.wiki.auth.UserManager) UserDatabase(org.apache.wiki.auth.user.UserDatabase) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) GroupManager(org.apache.wiki.auth.authorize.GroupManager)

Example 3 with WikiSecurityException

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

the class UserBean method doPost.

public String doPost(WikiContext context) {
    HttpServletRequest request = context.getHttpRequest();
    WikiSession session = context.getWikiSession();
    UserManager mgr = context.getEngine().getUserManager();
    String loginid = request.getParameter("loginid");
    String loginname = request.getParameter("loginname");
    String fullname = request.getParameter("fullname");
    String password = request.getParameter("password");
    String password2 = request.getParameter("password2");
    String email = request.getParameter("email");
    if (request.getParameter("action").equalsIgnoreCase("remove")) {
        try {
            mgr.getUserDatabase().deleteByLoginName(loginid);
            session.addMessage("User profile " + loginid + " (" + fullname + ") has been deleted");
        } catch (NoSuchPrincipalException e) {
            session.addMessage("User profile has already been removed");
        } catch (WikiSecurityException e) {
            session.addMessage("Security problem: " + e);
        }
        return "";
    }
    if (password != null && password.length() > 0 && !password.equals(password2)) {
        session.addMessage("Passwords do not match!");
        return "";
    }
    UserProfile p;
    if (loginid.equals("--New--")) {
        // Create new user
        p = mgr.getUserDatabase().newProfile();
        p.setCreated(new Date());
    } else {
        try {
            p = mgr.getUserDatabase().findByLoginName(loginid);
        } catch (NoSuchPrincipalException e) {
            session.addMessage("I could not find user profile " + loginid);
            return "";
        }
    }
    p.setEmail(email);
    p.setFullname(fullname);
    if (password != null && password.length() > 0)
        p.setPassword(password);
    p.setLoginName(loginname);
    try {
        mgr.getUserDatabase().save(p);
    } catch (WikiSecurityException e) {
        session.addMessage("Unable to save " + e.getMessage());
    }
    session.addMessage("User profile has been updated");
    return "";
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WikiSession(org.apache.wiki.WikiSession) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) UserProfile(org.apache.wiki.auth.user.UserProfile) UserManager(org.apache.wiki.auth.UserManager) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) Date(java.util.Date)

Example 4 with WikiSecurityException

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

the class AccessRuleLinkNodePostProcessorState method process.

/**
 * {@inheritDoc}
 *
 * @see NodePostProcessorState#process(NodeTracker, JSPWikiLink)
 */
@Override
public void process(final NodeTracker state, final JSPWikiLink link) {
    String ruleLine = NodePostProcessorStateCommonOperations.inlineLinkTextOnWysiwyg(state, link, m_wysiwygEditorMode);
    if (wikiContext.getEngine().getRenderingManager().getParser(wikiContext, link.getUrl().toString()).isParseAccessRules()) {
        final WikiPage page = wikiContext.getRealPage();
        if (ruleLine.startsWith("{")) {
            ruleLine = ruleLine.substring(1);
        }
        if (ruleLine.endsWith("}")) {
            ruleLine = ruleLine.substring(0, ruleLine.length() - 1);
        }
        LOG.debug("page=" + page.getName() + ", ACL = " + ruleLine);
        try {
            final Acl acl = wikiContext.getEngine().getAclManager().parseAcl(page, ruleLine);
            page.setAcl(acl);
            link.unlink();
            state.nodeRemoved(link);
            LOG.debug(acl.toString());
        } catch (final WikiSecurityException wse) {
            NodePostProcessorStateCommonOperations.makeError(state, link, wse.getMessage());
        }
    }
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) WikiPage(org.apache.wiki.WikiPage) Acl(org.apache.wiki.auth.acl.Acl)

Example 5 with WikiSecurityException

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

the class DefaultAclManager method setPermissions.

/**
 * Sets the access control list for the page and persists it by prepending
 * it to the wiki page markup and saving the page. When this method is
 * called, all other ACL markup in the page is removed. This method will forcibly
 * expire locks on the wiki page if they exist. Any ProviderExceptions will be
 * re-thrown as WikiSecurityExceptions.
 *
 * @param page the wiki page
 * @param acl  the access control list
 * @throws WikiSecurityException of the Acl cannot be set
 * @since 2.5
 */
public void setPermissions(WikiPage page, Acl acl) throws WikiSecurityException {
    PageManager pageManager = m_engine.getPageManager();
    // Forcibly expire any page locks
    PageLock lock = pageManager.getCurrentLock(page);
    if (lock != null) {
        pageManager.unlockPage(lock);
    }
    // Remove all of the existing ACLs.
    String pageText = m_engine.getPureText(page);
    Matcher matcher = DefaultAclManager.ACL_PATTERN.matcher(pageText);
    String cleansedText = matcher.replaceAll("");
    String newText = DefaultAclManager.printAcl(page.getAcl()) + cleansedText;
    try {
        pageManager.putPageText(page, newText);
    } catch (ProviderException e) {
        throw new WikiSecurityException("Could not set Acl. Reason: ProviderExcpetion " + e.getMessage(), e);
    }
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) PageManager(org.apache.wiki.PageManager) Matcher(java.util.regex.Matcher) ProviderException(org.apache.wiki.api.exceptions.ProviderException) PageLock(org.apache.wiki.PageLock)

Aggregations

WikiSecurityException (org.apache.wiki.auth.WikiSecurityException)28 NoSuchPrincipalException (org.apache.wiki.auth.NoSuchPrincipalException)10 IOException (java.io.IOException)8 Principal (java.security.Principal)7 NoRequiredPropertyException (org.apache.wiki.api.exceptions.NoRequiredPropertyException)7 WikiPrincipal (org.apache.wiki.auth.WikiPrincipal)7 NamingException (javax.naming.NamingException)6 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)5 Date (java.util.Date)5 UserProfile (org.apache.wiki.auth.user.UserProfile)3 Element (org.w3c.dom.Element)3 NodeList (org.w3c.dom.NodeList)3 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 ResultSet (java.sql.ResultSet)2 Timestamp (java.sql.Timestamp)2