Search in sources :

Example 1 with AppRole

use of samples.gae.security.AppRole in project spring-security by spring-projects.

the class GaeDatastoreUserRegistry method registerUser.

public void registerUser(GaeUser newUser) {
    logger.debug("Attempting to create new user " + newUser);
    Key key = KeyFactory.createKey(USER_TYPE, newUser.getUserId());
    Entity user = new Entity(key);
    user.setProperty(USER_EMAIL, newUser.getEmail());
    user.setProperty(USER_NICKNAME, newUser.getNickname());
    user.setProperty(USER_FORENAME, newUser.getForename());
    user.setProperty(USER_SURNAME, newUser.getSurname());
    user.setUnindexedProperty(USER_ENABLED, newUser.isEnabled());
    Collection<AppRole> roles = newUser.getAuthorities();
    long binaryAuthorities = 0;
    for (AppRole r : roles) {
        binaryAuthorities |= 1 << r.getBit();
    }
    user.setUnindexedProperty(USER_AUTHORITIES, binaryAuthorities);
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(user);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) AppRole(samples.gae.security.AppRole) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Key(com.google.appengine.api.datastore.Key)

Example 2 with AppRole

use of samples.gae.security.AppRole in project spring-security by spring-projects.

the class GaeDatastoreUserRegistry method findUser.

public GaeUser findUser(String userId) {
    Key key = KeyFactory.createKey(USER_TYPE, userId);
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    try {
        Entity user = datastore.get(key);
        long binaryAuthorities = (Long) user.getProperty(USER_AUTHORITIES);
        Set<AppRole> roles = EnumSet.noneOf(AppRole.class);
        for (AppRole r : AppRole.values()) {
            if ((binaryAuthorities & (1 << r.getBit())) != 0) {
                roles.add(r);
            }
        }
        GaeUser gaeUser = new GaeUser(user.getKey().getName(), (String) user.getProperty(USER_NICKNAME), (String) user.getProperty(USER_EMAIL), (String) user.getProperty(USER_FORENAME), (String) user.getProperty(USER_SURNAME), roles, (Boolean) user.getProperty(USER_ENABLED));
        return gaeUser;
    } catch (EntityNotFoundException e) {
        logger.debug(userId + " not found in datastore");
        return null;
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) AppRole(samples.gae.security.AppRole) DatastoreService(com.google.appengine.api.datastore.DatastoreService) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) Key(com.google.appengine.api.datastore.Key)

Example 3 with AppRole

use of samples.gae.security.AppRole in project spring-security by spring-projects.

the class RegistrationController method register.

@RequestMapping(method = RequestMethod.POST)
public String register(@Valid RegistrationForm form, BindingResult result) {
    if (result.hasErrors()) {
        return null;
    }
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    GaeUser currentUser = (GaeUser) authentication.getPrincipal();
    Set<AppRole> roles = EnumSet.of(AppRole.USER);
    if (UserServiceFactory.getUserService().isUserAdmin()) {
        roles.add(AppRole.ADMIN);
    }
    GaeUser user = new GaeUser(currentUser.getUserId(), currentUser.getNickname(), currentUser.getEmail(), form.getForename(), form.getSurname(), roles, true);
    registry.registerUser(user);
    // Update the context with the full authentication
    SecurityContextHolder.getContext().setAuthentication(new GaeUserAuthentication(user, authentication.getDetails()));
    return "redirect:/home.htm";
}
Also used : AppRole(samples.gae.security.AppRole) GaeUserAuthentication(samples.gae.security.GaeUserAuthentication) Authentication(org.springframework.security.core.Authentication) GaeUserAuthentication(samples.gae.security.GaeUserAuthentication) GaeUser(samples.gae.users.GaeUser) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

AppRole (samples.gae.security.AppRole)3 DatastoreService (com.google.appengine.api.datastore.DatastoreService)2 Entity (com.google.appengine.api.datastore.Entity)2 Key (com.google.appengine.api.datastore.Key)2 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)1 Authentication (org.springframework.security.core.Authentication)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 GaeUserAuthentication (samples.gae.security.GaeUserAuthentication)1 GaeUser (samples.gae.users.GaeUser)1