use of org.activityinfo.server.database.hibernate.entity.User in project activityinfo by bedatadriven.
the class BasicAuthentication method authenticate.
// This method checks the user information sent in the Authorization
// header against the database of users maintained in the users Hashtable.
public User authenticate(String auth) throws IOException {
if (Strings.isNullOrEmpty(auth)) {
// no auth
return null;
}
if (!auth.toUpperCase().startsWith("BASIC ")) {
LOGGER.severe("Unsupported authorization header [" + auth + "]");
// we only do BASIC
return null;
}
// Get encoded user and password, comes after "BASIC "
String emailPasswordEncoded = auth.substring(6);
// Decode it, using any base 64 decoder
byte[] emailPassDecodedBytes = BaseEncoding.base64().decode(emailPasswordEncoded);
String emailPassDecoded = new String(emailPassDecodedBytes, Charsets.UTF_8);
String[] emailPass = emailPassDecoded.split(":");
if (emailPass.length != 2) {
return null;
}
// look up the user in the database
User user = null;
try {
user = userDAO.get().findUserByEmail(emailPass[0]);
} catch (NoResultException e) {
return null;
}
if (!authenticator.get().check(user, emailPass[1])) {
return null;
}
return user;
}
use of org.activityinfo.server.database.hibernate.entity.User in project activityinfo by bedatadriven.
the class BasicAuthentication method doAuthentication.
public User doAuthentication(String auth) throws IOException {
User user = authenticate(auth);
if (user == null) {
return null;
}
authProvider.set(new AuthenticatedUser("", user.getId(), user.getEmail()));
return user;
}
use of org.activityinfo.server.database.hibernate.entity.User in project activityinfo by bedatadriven.
the class HumanitarianId method humanitarianIdAuthorized.
@GET
@Path(HID_REDIRECT_PATH)
public Response humanitarianIdAuthorized(@Context UriInfo uriInfo, @QueryParam("code") String code, @QueryParam("state") String state) throws IOException {
if (!flow.isPresent()) {
return serviceUnvailable();
}
// First exchange our authorization code for an access token that we can use
// to request information about the user that has just logged in
TokenResponse tokenResponse = flow.get().newTokenRequest(code).setScopes(Collections.singletonList("profile")).setGrantType("authorization_code").setTokenServerUrl(new GenericUrl("https://auth.humanitarian.id/oauth/access_token")).execute();
// Now query the user's email address and name
URL accountUrl = UriBuilder.fromUri("https://auth.humanitarian.id/account.json").queryParam("access_token", tokenResponse.getAccessToken()).build().toURL();
URLFetchService fetchService = URLFetchServiceFactory.getURLFetchService();
HTTPResponse response = fetchService.fetch(accountUrl);
HumanitarianIdAccount account = HumanitarianIdAccount.parse(response.getContent());
// Now lookup the user's email address in our directory to see if this user already has an account
List<User> existingUser = entityManager.get().createQuery("SELECT u FROM User u WHERE u.email = :email", User.class).setParameter("email", account.getEmail()).getResultList();
if (existingUser.isEmpty()) {
// we are trusting humanitarian.id to verify the user's email address
return createNewAccount(uriInfo.getBaseUri(), account);
} else {
return redirectToApp(uriInfo.getBaseUri(), existingUser.get(0));
}
}
use of org.activityinfo.server.database.hibernate.entity.User in project activityinfo by bedatadriven.
the class UpdateUserPermissionsHandler method createNewUser.
private User createNewUser(User executingUser, UserPermissionDTO dto) {
if (executingUser.getId() == 0) {
throw new AssertionError("executingUser.id == 0!");
}
if (executingUser.getName() == null) {
throw new AssertionError("executingUser.name == null!");
}
User user = UserDAOImpl.createNewUser(dto.getEmail(), dto.getName(), executingUser.getLocale());
user.setInvitedBy(executingUser);
userDAO.persist(user);
try {
Message message = mailSender.createMessage(new InvitationMessage(user, executingUser));
message.replyTo(executingUser.getEmail(), executingUser.getName());
mailSender.send(message);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Could not send invitation mail", e);
throw new CommandException("Failed to send invitation email");
}
return user;
}
use of org.activityinfo.server.database.hibernate.entity.User in project activityinfo by bedatadriven.
the class ActivityDigestModelBuilder method createDatabaseModel.
private void createDatabaseModel(ActivityDigestModel model, Database database) {
SiteHistory lastEdit = findLastEdit(database);
// only include databases that are known to be edited at least once
if (lastEdit != null) {
DatabaseModel databaseModel = new DatabaseModel(model, database, lastEdit);
List<SiteHistory> ownerHistories = findSiteHistory(databaseModel, database.getOwner());
ActivityMap ownerActivityMap = new ActivityMap(databaseModel, database.getOwner(), ownerHistories);
databaseModel.setOwnerActivityMap(ownerActivityMap);
List<Partner> partners = findPartners(databaseModel);
LOGGER.finest("building user activity digest for user " + model.getUserDigest().getUser().getId() + " and database " + database.getId() + " - found " + partners.size() + " partner(s)");
if (!partners.isEmpty()) {
for (Partner partner : partners) {
PartnerActivityModel partnerModel = new PartnerActivityModel(databaseModel, partner);
List<User> partnerUsers = findUsers(partnerModel);
LOGGER.finest("found users " + partnerUsers + " for partner " + partner.getName());
if (!partnerUsers.isEmpty()) {
for (User partnerUser : partnerUsers) {
List<SiteHistory> histories = findSiteHistory(databaseModel, partnerUser);
ActivityMap activityMap = new ActivityMap(databaseModel, partnerUser, histories);
partnerModel.addActivityMap(activityMap);
}
}
}
}
}
}
Aggregations