Java Code Examples for org.rapidoid.setup.My#rolesProvider()

The following examples show how to use org.rapidoid.setup.My#rolesProvider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestServer.java    From apollo with Apache License 2.0 6 votes vote down vote up
private void registerRolesProvider() {
    My.rolesProvider((req, username) -> {
        try {
            User user = userDao.getUser(username);
            if (!user.isEnabled()) {
                req.response().code(HttpStatus.FORBIDDEN);
                return null;
            }
            if (user.isAdmin()) {
                return U.set(Role.ADMINISTRATOR);
            }
            return U.set(Role.ANYBODY);

        } catch (Exception e) {
            logger.error("Got exception while getting user roles! setting to ANYBODY", e);
            return U.set(Role.ANYBODY);
        }
    });
}
 
Example 2
Source File: Main.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	App.bootstrap(args);

	Boot.auth();

	On.get("/").html((req, resp) -> "this is public!");

	On.get("/manage").roles("manager").html((req, resp) -> "this is private!");

	/* Dummy login: successful if the username is the same as the password */

	My.loginProvider((req, username, password) -> username.equals(password));

	/* Gives the 'manager' role to every logged-in user */

	My.rolesProvider((req, username) -> U.set("manager"));
}
 
Example 3
Source File: Main.java    From rapidoid with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {
	App.bootstrap(args);

	My.rolesProvider((req, username) -> username.equals("bob") ? U.set("manager") : U.set());

	On.get("/hey").roles("manager").json(() -> U.map("msg", "ok"));

	// generate a token
	String token = Tokens.serialize(U.map("_user", "bob"));

	// demo request, prints {"msg":"ok"}
	Self.get("/hey?_token=" + token).print();
}