use of org.apache.wiki.WikiSession in project jspwiki by apache.
the class AuthenticationManagerTest method testLoginCustom.
@Test
public void testLoginCustom() throws Exception {
WikiSession session = WikiSessionTest.authenticatedSession(m_engine, Users.JANNE, Users.JANNE_PASS);
Assert.assertTrue(session.hasPrincipal(Role.ALL));
Assert.assertTrue(session.hasPrincipal(Role.AUTHENTICATED));
Assert.assertTrue(session.hasPrincipal(new WikiPrincipal(Users.JANNE, WikiPrincipal.LOGIN_NAME)));
Assert.assertTrue(session.hasPrincipal(new WikiPrincipal("JanneJalkanen", WikiPrincipal.WIKI_NAME)));
Assert.assertTrue(session.hasPrincipal(new WikiPrincipal("Janne Jalkanen", WikiPrincipal.FULL_NAME)));
}
use of org.apache.wiki.WikiSession in project jspwiki by apache.
the class AuthenticationManagerTest method testLoginCustomWithGroup.
@Test
public void testLoginCustomWithGroup() throws Exception {
// perhaps)
try {
m_groupMgr.removeGroup("Test1");
m_groupMgr.removeGroup("Test2");
} catch (NoSuchPrincipalException e) {
}
// Log in 'janne' and verify there are 5 principals in the subject
// (ALL, AUTHENTICATED, login, fullname, wikiname Principals)
WikiSession session = WikiSession.guestSession(m_engine);
m_auth.login(session, null, Users.JANNE, Users.JANNE_PASS);
Assert.assertEquals(3, session.getPrincipals().length);
Assert.assertEquals(2, session.getRoles().length);
Assert.assertTrue(session.hasPrincipal(new WikiPrincipal("JanneJalkanen", WikiPrincipal.WIKI_NAME)));
// Listen for any manager group-add events
GroupManager manager = m_engine.getGroupManager();
SecurityEventTrap trap = new SecurityEventTrap();
manager.addWikiEventListener(trap);
// Create two groups; one with Janne in it, and one without
Group groupTest1 = m_groupMgr.parseGroup("Test1", "JanneJalkanen \n Bob \n Charlie", true);
m_groupMgr.setGroup(m_session, groupTest1);
groupTest1 = m_groupMgr.getGroup("Test1");
Principal principalTest1 = groupTest1.getPrincipal();
Group groupTest2 = m_groupMgr.parseGroup("Test2", "Alice \n Bob \n Charlie", true);
m_groupMgr.setGroup(m_session, groupTest2);
groupTest2 = m_groupMgr.getGroup("Test2");
Principal principalTest2 = groupTest2.getPrincipal();
// We should see two security events (one for each group create)
// We should also see a GroupPrincipal for group Test1, but not Test2
Assert.assertEquals(2, trap.events().length);
Assert.assertTrue(session.hasPrincipal(principalTest1));
Assert.assertFalse(session.hasPrincipal(principalTest2));
// If we remove Test1, the GroupPrincipal should disappear
m_groupMgr.removeGroup("Test1");
Assert.assertFalse(session.hasPrincipal(principalTest1));
Assert.assertFalse(session.hasPrincipal(principalTest2));
// Now, add 'JanneJalkanen' to Test2 group manually; we should see the
// GroupPrincipal
groupTest2.add(new WikiPrincipal("JanneJalkanen"));
m_groupMgr.setGroup(session, groupTest2);
Assert.assertFalse(session.hasPrincipal(principalTest1));
Assert.assertTrue(session.hasPrincipal(principalTest2));
// Remove 'JanneJalkenen' manually; the GroupPrincipal should disappear
groupTest2.remove(new WikiPrincipal("JanneJalkanen"));
m_groupMgr.setGroup(session, groupTest2);
Assert.assertFalse(session.hasPrincipal(principalTest1));
Assert.assertFalse(session.hasPrincipal(principalTest2));
// Clean up
m_groupMgr.removeGroup("Test2");
}
use of org.apache.wiki.WikiSession 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 "";
}
use of org.apache.wiki.WikiSession in project jspwiki by apache.
the class UserCheckTag method doWikiStartTag.
/**
* {@inheritDoc}
* @see org.apache.wiki.tags.WikiTagBase#doWikiStartTag()
*/
@Override
public final int doWikiStartTag() throws IOException {
WikiSession session = m_wikiContext.getWikiSession();
String status = session.getStatus();
AuthenticationManager mgr = m_wikiContext.getEngine().getAuthenticationManager();
boolean containerAuth = mgr.isContainerAuthenticated();
boolean cookieAssertions = mgr.allowsCookieAssertions();
if (m_status != null) {
if (ANONYMOUS.equals(m_status)) {
if (status.equals(WikiSession.ANONYMOUS)) {
return EVAL_BODY_INCLUDE;
}
} else if (AUTHENTICATED.equals(m_status)) {
if (status.equals(WikiSession.AUTHENTICATED)) {
return EVAL_BODY_INCLUDE;
}
} else if (ASSERTED.equals(m_status)) {
if (status.equals(WikiSession.ASSERTED)) {
return EVAL_BODY_INCLUDE;
}
} else if (ASSERTIONS_ALLOWED.equals(m_status)) {
if (cookieAssertions) {
return EVAL_BODY_INCLUDE;
}
return SKIP_BODY;
} else if (ASSERTIONS_NOT_ALLOWED.equals(m_status)) {
if (!cookieAssertions) {
return EVAL_BODY_INCLUDE;
}
return SKIP_BODY;
} else if (CONTAINER_AUTH.equals(m_status)) {
if (containerAuth) {
return EVAL_BODY_INCLUDE;
}
return SKIP_BODY;
} else if (CUSTOM_AUTH.equals(m_status)) {
if (!containerAuth) {
return EVAL_BODY_INCLUDE;
}
return SKIP_BODY;
} else if (KNOWN.equals(m_status)) {
if (!session.isAnonymous()) {
return EVAL_BODY_INCLUDE;
}
return SKIP_BODY;
} else if (NOT_AUTHENTICATED.equals(m_status)) {
if (!status.equals(WikiSession.AUTHENTICATED)) {
return EVAL_BODY_INCLUDE;
}
}
}
return SKIP_BODY;
}
use of org.apache.wiki.WikiSession in project jspwiki by apache.
the class PermissionTag method checkPermission.
/**
* Checks a single permission.
*
* @param permission
* @return true if granted, false if not
*/
private boolean checkPermission(String permission) {
WikiSession session = m_wikiContext.getWikiSession();
WikiPage page = m_wikiContext.getPage();
AuthorizationManager mgr = m_wikiContext.getEngine().getAuthorizationManager();
boolean gotPermission = false;
if (CREATE_GROUPS.equals(permission) || CREATE_PAGES.equals(permission) || EDIT_PREFERENCES.equals(permission) || EDIT_PROFILE.equals(permission) || LOGIN.equals(permission)) {
gotPermission = mgr.checkPermission(session, new WikiPermission(page.getWiki(), permission));
} else if (VIEW_GROUP.equals(permission) || EDIT_GROUP.equals(permission) || DELETE_GROUP.equals(permission)) {
Command command = m_wikiContext.getCommand();
gotPermission = false;
if (command instanceof GroupCommand && command.getTarget() != null) {
GroupPrincipal group = (GroupPrincipal) command.getTarget();
String groupName = group.getName();
String action = "view";
if (EDIT_GROUP.equals(permission)) {
action = "edit";
} else if (DELETE_GROUP.equals(permission)) {
action = "delete";
}
gotPermission = mgr.checkPermission(session, new GroupPermission(groupName, action));
}
} else if (ALL_PERMISSION.equals(permission)) {
gotPermission = mgr.checkPermission(session, new AllPermission(m_wikiContext.getEngine().getApplicationName()));
} else if (page != null) {
//
if (EDIT.equals(permission)) {
WikiPage latest = m_wikiContext.getEngine().getPage(page.getName());
if (page.getVersion() != WikiProvider.LATEST_VERSION && latest.getVersion() != page.getVersion()) {
return false;
}
}
Permission p = PermissionFactory.getPagePermission(page, permission);
gotPermission = mgr.checkPermission(session, p);
}
return gotPermission;
}
Aggregations