use of org.apache.shiro.subject.Subject in project zeppelin by apache.
the class LoginRestApi method logout.
@POST
@Path("logout")
@ZeppelinApi
public Response logout() {
JsonResponse response;
Subject currentUser = org.apache.shiro.SecurityUtils.getSubject();
currentUser.logout();
response = new JsonResponse(Response.Status.UNAUTHORIZED, "", "");
LOG.warn(response.toString());
return response.build();
}
use of org.apache.shiro.subject.Subject in project zeppelin by apache.
the class SecurityUtils method getRoles.
/**
* Return the roles associated with the authenticated user if any otherwise returns empty set
* TODO(prasadwagle) Find correct way to get user roles (see SHIRO-492)
*
* @return shiro roles
*/
public static HashSet<String> getRoles() {
if (!isEnabled) {
return EMPTY_HASHSET;
}
Subject subject = org.apache.shiro.SecurityUtils.getSubject();
HashSet<String> roles = new HashSet<>();
Map allRoles = null;
if (subject.isAuthenticated()) {
Collection realmsList = SecurityUtils.getRealmsList();
for (Iterator<Realm> iterator = realmsList.iterator(); iterator.hasNext(); ) {
Realm realm = iterator.next();
String name = realm.getClass().getName();
if (name.equals("org.apache.shiro.realm.text.IniRealm")) {
allRoles = ((IniRealm) realm).getIni().get("roles");
break;
} else if (name.equals("org.apache.zeppelin.realm.LdapRealm")) {
allRoles = ((LdapRealm) realm).getListRoles();
break;
}
}
if (allRoles != null) {
Iterator it = allRoles.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
if (subject.hasRole((String) pair.getKey())) {
roles.add((String) pair.getKey());
}
}
}
}
return roles;
}
use of org.apache.shiro.subject.Subject in project qi4j-sdk by Qi4j.
the class PasswordDomainTest method test.
// END SNIPPET: assembly
@Test
public void test() throws UnitOfWorkCompletionException {
UnitOfWork uow = module.newUnitOfWork();
UserFactory userFactory = module.findService(UserFactory.class).get();
// START SNIPPET: usage
User user = userFactory.createNewUser("foo", "bar");
// END SNIPPET: usage
uow.complete();
uow = module.newUnitOfWork();
// START SNIPPET: usage
Subject currentUser = SecurityUtils.getSubject();
currentUser.login(new UsernamePasswordToken("foo", "bar"));
// END SNIPPET: usage
assertNotNull("Unable to authenticate against PasswordRealmService", currentUser.getPrincipal());
assertFalse(currentUser.hasRole("role-one"));
uow.discard();
}
use of org.apache.shiro.subject.Subject in project qi4j-sdk by Qi4j.
the class PermissionsDomainTest method test.
@Test
public void test() throws UnitOfWorkCompletionException {
// START SNIPPET: usage
UnitOfWork uow = module.newUnitOfWork();
User user = userFactory.createNewUser("foo", "bar");
Role role = roleFactory.create("role-one", "permission-one", "permission-two");
role.assignTo(user);
uow.complete();
// END SNIPPET: usage
// START SNIPPET: usage
uow = module.newUnitOfWork();
Subject currentUser = SecurityUtils.getSubject();
currentUser.login(new UsernamePasswordToken("foo", "bar"));
if (!currentUser.hasRole("role-one")) {
fail("User 'foo' must have 'role-one' role.");
}
if (!currentUser.isPermitted("permission-one")) {
fail("User 'foo' must have 'permission-one' permission.");
}
// END SNIPPET: usage
assertThat(currentUser.hasRole("role-one"), is(true));
assertThat(currentUser.hasRole("role-two"), is(false));
assertThat(currentUser.isPermitted("permission-one"), is(true));
assertThat(currentUser.isPermitted("permission-two"), is(true));
assertThat(currentUser.isPermitted("permission-three"), is(false));
// START SNIPPET: usage
uow.discard();
// END SNIPPET: usage
}
use of org.apache.shiro.subject.Subject in project qi4j-sdk by Qi4j.
the class RealmServiceTest method test.
// END SNIPPET: realm-service
@Test
public void test() {
Subject currentUser = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("foo", "bar");
currentUser.login(token);
assertNotNull("Unable to authenticate against MyRealmService", currentUser.getPrincipal());
}
Aggregations