use of ai.elimu.model.contributor.SignOnEvent in project webapp by elimu-ai.
the class SignOnControllerSelenium method handleRequest.
@RequestMapping(value = "/sign-on/test/role-{role}", method = RequestMethod.GET)
public String handleRequest(@PathVariable Role role, HttpServletRequest request, Model model) {
logger.info("handleRequest");
if (EnvironmentContextLoaderListener.env == Environment.PROD) {
return "redirect:/sign-on";
}
logger.info("role: " + role);
Contributor contributor = new Contributor();
contributor.setEmail("info+role-" + role + "@elimu.ai");
contributor.setRoles(new HashSet<>(Arrays.asList(role)));
contributor.setRegistrationTime(Calendar.getInstance());
contributor.setFirstName("TestRole");
contributor.setLastName(role.toString());
contributor.setLocale(Locale.EN);
contributor.setTeams(new HashSet<>(Arrays.asList(Team.TESTING)));
contributor.setMotivation("Regression testing as " + role);
Contributor existingContributor = contributorDao.read(contributor.getEmail());
logger.info("existingContributor: " + existingContributor);
if (existingContributor != null) {
contributor = existingContributor;
} else {
contributorDao.create(contributor);
logger.info("Contributor " + contributor.getEmail() + " was created at " + request.getServerName());
}
// Authenticate
new CustomAuthenticationManager().authenticateUser(contributor);
// Add Contributor object to session
request.getSession().setAttribute("contributor", contributor);
SignOnEvent signOnEvent = new SignOnEvent();
signOnEvent.setContributor(contributor);
signOnEvent.setCalendar(Calendar.getInstance());
signOnEvent.setServerName(request.getServerName());
signOnEvent.setProvider(Provider.SELENIUM);
signOnEvent.setRemoteAddress(request.getRemoteAddr());
signOnEvent.setUserAgent(StringUtils.abbreviate(request.getHeader("User-Agent"), 1000));
signOnEvent.setReferrer(CookieHelper.getReferrer(request));
signOnEvent.setUtmSource(CookieHelper.getUtmSource(request));
signOnEvent.setUtmMedium(CookieHelper.getUtmMedium(request));
signOnEvent.setUtmCampaign(CookieHelper.getUtmCampaign(request));
signOnEvent.setUtmTerm(CookieHelper.getUtmTerm(request));
signOnEvent.setReferralId(CookieHelper.getReferralId(request));
signOnEventDao.create(signOnEvent);
return "redirect:/content";
}
use of ai.elimu.model.contributor.SignOnEvent in project webapp by elimu-ai.
the class AddEmailController method handleSubmit.
@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpSession session, @RequestParam String email, Model model) {
logger.info("handleSubmit");
if (!EmailValidator.getInstance().isValid(email)) {
// TODO: display error message
return "content/contributor/add-email";
}
Contributor existingContributor = contributorDao.read(email);
if (existingContributor != null) {
// TODO: display error message
return "content/contributor/add-email";
}
Contributor contributor = (Contributor) session.getAttribute("contributor");
boolean isRedirectFromRegistrationPage = contributor.getEmail() == null;
contributor.setEmail(email);
contributorDao.create(contributor);
session.setAttribute("contributor", contributor);
SignOnEvent signOnEvent = new SignOnEvent();
signOnEvent.setContributor(contributor);
signOnEvent.setCalendar(Calendar.getInstance());
signOnEventDao.create(signOnEvent);
if (isRedirectFromRegistrationPage) {
// Send welcome e-mail
String to = contributor.getEmail();
String from = "elimu.ai <info@elimu.ai>";
String subject = "Welcome to the community";
String title = "Welcome!";
String firstName = StringUtils.isBlank(contributor.getFirstName()) ? "" : contributor.getFirstName();
String htmlText = "<p>Hi, " + firstName + "</p>";
htmlText += "<p>Thank you very much for registering as a contributor to the elimu.ai community. We are glad to see you join us!</p>";
htmlText += "<p>With your help, this is what we aim to achieve:</p>";
htmlText += "<p><blockquote>\"The mission of the elimu.ai project is to build software that will enable children without access to school to learn how to read and write <i>on their own</i>.\"</blockquote></p>";
htmlText += "<p><img src=\"http://elimu.ai/static/img/banner-en.jpg\" alt=\"\" style=\"width: 564px; max-width: 100%;\" /></p>";
htmlText += "<h2>Chat</h2>";
htmlText += "<p>At http://slack.elimu.ai you can chat with the other community members.</p>";
htmlText += "<h2>Feedback</h2>";
htmlText += "<p>If you have any questions or suggestions, please contact us by replying to this e-mail or messaging us in the Slack chat room.</p>";
Mailer.sendHtml(to, null, from, subject, title, htmlText);
if (EnvironmentContextLoaderListener.env == Environment.PROD) {
// Post notification in Slack
String name = "";
if (StringUtils.isNotBlank(contributor.getFirstName())) {
name += "(";
name += contributor.getFirstName();
if (StringUtils.isNotBlank(contributor.getLastName())) {
name += " " + contributor.getLastName();
}
name += ")";
}
String text = URLEncoder.encode("A new contributor " + name + " just joined the community: ") + "http://elimu.ai/content/community/contributors";
String iconUrl = contributor.getImageUrl();
SlackApiHelper.postMessage(null, text, iconUrl, null);
}
}
return "redirect:/content";
}
use of ai.elimu.model.contributor.SignOnEvent in project webapp by elimu-ai.
the class SignOnController method handleOfflineSignOnRequest.
/**
* To make it possible to sign on without an active Internet connection,
* enable sign-on with a test user during offline development.
*/
@RequestMapping("/offline")
public String handleOfflineSignOnRequest(HttpServletRequest request) {
logger.info("handleOfflineSignOnRequest");
if (EnvironmentContextLoaderListener.env == Environment.DEV) {
// Create and store test user in database
Contributor contributor = contributorDao.read("test@elimu.ai");
if (contributor == null) {
contributor = new Contributor();
contributor.setEmail("test@elimu.ai");
contributor.setFirstName("Test");
contributor.setLastName("Contributor");
contributor.setRoles(new HashSet<>(Arrays.asList(Role.ADMIN, Role.ANALYST, Role.CONTRIBUTOR, Role.PROJECT_MANAGER)));
contributor.setRegistrationTime(Calendar.getInstance());
contributorDao.create(contributor);
}
// Authenticate
new CustomAuthenticationManager().authenticateUser(contributor);
// Add Contributor object to session
request.getSession().setAttribute("contributor", contributor);
SignOnEvent signOnEvent = new SignOnEvent();
signOnEvent.setContributor(contributor);
signOnEvent.setCalendar(Calendar.getInstance());
signOnEvent.setServerName(request.getServerName());
signOnEvent.setProvider(Provider.OFFLINE);
signOnEvent.setRemoteAddress(request.getRemoteAddr());
signOnEvent.setUserAgent(StringUtils.abbreviate(request.getHeader("User-Agent"), 1000));
signOnEvent.setReferrer(CookieHelper.getReferrer(request));
signOnEvent.setUtmSource(CookieHelper.getUtmSource(request));
signOnEvent.setUtmMedium(CookieHelper.getUtmMedium(request));
signOnEvent.setUtmCampaign(CookieHelper.getUtmCampaign(request));
signOnEvent.setUtmTerm(CookieHelper.getUtmTerm(request));
signOnEvent.setReferralId(CookieHelper.getReferralId(request));
signOnEventDao.create(signOnEvent);
return "redirect:/content";
} else {
return null;
}
}
use of ai.elimu.model.contributor.SignOnEvent in project webapp by elimu-ai.
the class SignOnControllerGitHub method handleCallback.
/**
* See https://developer.github.com/v3/oauth/#2-github-redirects-back-to-your-site
*/
@RequestMapping(value = "/sign-on/github/callback", method = RequestMethod.GET)
public String handleCallback(HttpServletRequest request, Model model) {
logger.info("handleCallback");
String state = request.getParameter("state");
logger.debug("state: " + state);
if (!secretState.equals(state)) {
return "redirect:/sign-on?error=state_mismatch";
} else {
String code = request.getParameter("code");
logger.debug("verifierParam: " + code);
String responseBody = null;
try {
OAuth2AccessToken accessToken = oAuth20Service.getAccessToken(code);
logger.debug("accessToken: " + accessToken);
OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
oAuth20Service.signRequest(accessToken, oAuthRequest);
Response response = oAuth20Service.execute(oAuthRequest);
responseBody = response.getBody();
logger.info("response.getCode(): " + response.getCode());
logger.info("response.getBody(): " + responseBody);
} catch (IOException | InterruptedException | ExecutionException ex) {
logger.error(null, ex);
return "redirect:/sign-on?login_error=" + ex.getMessage();
}
Contributor contributor = new Contributor();
contributor.setReferrer(CookieHelper.getReferrer(request));
contributor.setUtmSource(CookieHelper.getUtmSource(request));
contributor.setUtmMedium(CookieHelper.getUtmMedium(request));
contributor.setUtmCampaign(CookieHelper.getUtmCampaign(request));
contributor.setUtmTerm(CookieHelper.getUtmTerm(request));
contributor.setReferralId(CookieHelper.getReferralId(request));
try {
JSONObject jsonObject = new JSONObject(responseBody);
logger.info("jsonObject: " + jsonObject);
if (jsonObject.has("email")) {
if (!jsonObject.isNull("email")) {
// TODO: validate e-mail
contributor.setEmail(jsonObject.getString("email"));
}
}
if (jsonObject.has("login")) {
contributor.setUsernameGitHub(jsonObject.getString("login"));
}
if (jsonObject.has("id")) {
Long idAsLong = jsonObject.getLong("id");
String id = String.valueOf(idAsLong);
contributor.setProviderIdGitHub(id);
}
if (jsonObject.has("avatar_url")) {
contributor.setImageUrl(jsonObject.getString("avatar_url"));
}
if (jsonObject.has("name")) {
if (!jsonObject.isNull("name")) {
String name = jsonObject.getString("name");
String[] nameParts = name.split(" ");
String firstName = nameParts[0];
logger.info("firstName: " + firstName);
contributor.setFirstName(firstName);
if (nameParts.length > 1) {
String lastName = nameParts[nameParts.length - 1];
logger.info("lastName: " + lastName);
contributor.setLastName(lastName);
}
}
}
} catch (JSONException e) {
logger.error(null, e);
}
Contributor existingContributor = contributorDao.read(contributor.getEmail());
if (existingContributor == null) {
// Look for existing Contributor with matching GitHub id
existingContributor = contributorDao.readByProviderIdGitHub(contributor.getProviderIdGitHub());
}
if (existingContributor == null) {
// Store new Contributor in database
contributor.setRegistrationTime(Calendar.getInstance());
if (StringUtils.isNotBlank(contributor.getEmail()) && contributor.getEmail().endsWith("@elimu.ai")) {
contributor.setRoles(new HashSet<>(Arrays.asList(Role.ADMIN, Role.ANALYST, Role.CONTRIBUTOR)));
} else {
contributor.setRoles(new HashSet<>(Arrays.asList(Role.CONTRIBUTOR)));
}
if (contributor.getEmail() == null) {
request.getSession().setAttribute("contributor", contributor);
new CustomAuthenticationManager().authenticateUser(contributor);
return "redirect:/content/contributor/add-email";
}
contributorDao.create(contributor);
// Send welcome e-mail
String to = contributor.getEmail();
String from = "elimu.ai <info@elimu.ai>";
String subject = "Welcome to the community";
String title = "Welcome!";
String firstName = StringUtils.isBlank(contributor.getFirstName()) ? "" : contributor.getFirstName();
String htmlText = "<p>Hi, " + firstName + "</p>";
htmlText += "<p>Thank you very much for registering as a contributor to the elimu.ai community. We are glad to see you join us!</p>";
htmlText += "<h2>Purpose</h2>";
htmlText += "<p>The purpose of elimu.ai is to provide <i>every child</i> with access to quality basic education.</p>";
htmlText += "<h2>Why?</h2>";
htmlText += "<p>The word \"elimu\" is Swahili for \"education\". We believe that a free quality education is the right of every child no matter her social or geographical background.</p>";
htmlText += "<h2>How?</h2>";
htmlText += "<p>With your help, this is what we aim to achieve:</p>";
htmlText += "<p><blockquote>\"We build tablet-based software that teaches a child to read, write and calculate fully autonomously, without guidance from qualified teachers.\"</blockquote></p>";
htmlText += "<p><img src=\"http://elimu.ai/static/img/banner-en.jpg\" alt=\"\" style=\"width: 564px; max-width: 100%;\" /></p>";
htmlText += "<h2>Chat</h2>";
htmlText += "<p>At http://slack.elimu.ai you can chat with the other community members.</p>";
Mailer.sendHtmlWithButton(to, null, from, subject, title, htmlText, "Open chat", "http://slack.elimu.ai");
if (EnvironmentContextLoaderListener.env == Environment.PROD) {
// Post notification in Slack
String name = "";
if (StringUtils.isNotBlank(contributor.getFirstName())) {
name += "(";
name += contributor.getFirstName();
if (StringUtils.isNotBlank(contributor.getLastName())) {
name += " " + contributor.getLastName();
}
name += ")";
}
String text = URLEncoder.encode("A new contributor " + name + " just joined the community: ") + "http://elimu.ai/content/community/contributors";
String iconUrl = contributor.getImageUrl();
SlackApiHelper.postMessage(null, text, iconUrl, null);
}
} else {
// Update existing contributor with latest values fetched from provider
if (StringUtils.isNotBlank(contributor.getUsernameGitHub())) {
existingContributor.setUsernameGitHub(contributor.getUsernameGitHub());
}
if (StringUtils.isNotBlank(contributor.getProviderIdGitHub())) {
existingContributor.setProviderIdGitHub(contributor.getProviderIdGitHub());
}
if (StringUtils.isNotBlank(contributor.getImageUrl())) {
existingContributor.setImageUrl(contributor.getImageUrl());
}
// TODO: firstName/lastName
if (StringUtils.isBlank(existingContributor.getReferrer())) {
existingContributor.setReferrer(contributor.getReferrer());
}
if (StringUtils.isBlank(existingContributor.getUtmSource())) {
existingContributor.setUtmSource(contributor.getUtmSource());
}
if (StringUtils.isBlank(existingContributor.getUtmMedium())) {
existingContributor.setUtmMedium(contributor.getUtmMedium());
}
if (StringUtils.isBlank(existingContributor.getUtmCampaign())) {
existingContributor.setUtmCampaign(contributor.getUtmCampaign());
}
if (StringUtils.isBlank(existingContributor.getUtmTerm())) {
existingContributor.setUtmTerm(contributor.getUtmTerm());
}
if (existingContributor.getReferralId() == null) {
existingContributor.setReferralId(contributor.getReferralId());
}
contributorDao.update(existingContributor);
// Contributor registered previously
contributor = existingContributor;
}
// Authenticate
new CustomAuthenticationManager().authenticateUser(contributor);
// Add Contributor object to session
request.getSession().setAttribute("contributor", contributor);
SignOnEvent signOnEvent = new SignOnEvent();
signOnEvent.setContributor(contributor);
signOnEvent.setCalendar(Calendar.getInstance());
signOnEvent.setServerName(request.getServerName());
signOnEvent.setProvider(Provider.GITHUB);
signOnEvent.setRemoteAddress(request.getRemoteAddr());
signOnEvent.setUserAgent(StringUtils.abbreviate(request.getHeader("User-Agent"), 1000));
signOnEvent.setReferrer(CookieHelper.getReferrer(request));
signOnEvent.setUtmSource(CookieHelper.getUtmSource(request));
signOnEvent.setUtmMedium(CookieHelper.getUtmMedium(request));
signOnEvent.setUtmCampaign(CookieHelper.getUtmCampaign(request));
signOnEvent.setUtmTerm(CookieHelper.getUtmTerm(request));
signOnEvent.setReferralId(CookieHelper.getReferralId(request));
signOnEventDao.create(signOnEvent);
return "redirect:/content";
}
}
use of ai.elimu.model.contributor.SignOnEvent in project webapp by elimu-ai.
the class SignOnControllerGoogle method handleCallback.
@RequestMapping(value = "/sign-on/google/callback", method = RequestMethod.GET)
public String handleCallback(HttpServletRequest request, Model model) {
logger.info("handleCallback");
if (request.getParameter("error") != null) {
return "redirect:/sign-on?error=" + request.getParameter("error");
} else {
String verifierParam = request.getParameter("code");
logger.debug("verifierParam: " + verifierParam);
Verifier verifier = new Verifier(verifierParam);
String responseBody = null;
try {
Token accessToken = oAuthService.getAccessToken(requestToken, verifier);
logger.debug("accessToken: " + accessToken);
OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, "https://www.googleapis.com/oauth2/v1/userinfo");
oAuthService.signRequest(accessToken, oAuthRequest);
Response response = oAuthRequest.send();
responseBody = response.getBody();
logger.info("response.getCode(): " + response.getCode());
logger.info("response.getBody(): " + responseBody);
} catch (OAuthException e) {
logger.error(null, e);
return "redirect:/sign-on?login_error=" + e.getMessage();
}
Contributor contributor = new Contributor();
contributor.setReferrer(CookieHelper.getReferrer(request));
contributor.setUtmSource(CookieHelper.getUtmSource(request));
contributor.setUtmMedium(CookieHelper.getUtmMedium(request));
contributor.setUtmCampaign(CookieHelper.getUtmCampaign(request));
contributor.setUtmTerm(CookieHelper.getUtmTerm(request));
contributor.setReferralId(CookieHelper.getReferralId(request));
try {
JSONObject jsonObject = new JSONObject(responseBody);
logger.info("jsonObject: " + jsonObject);
if (jsonObject.has("email")) {
// TODO: validate e-mail
contributor.setEmail(jsonObject.getString("email"));
}
if (jsonObject.has("id")) {
contributor.setProviderIdGoogle(jsonObject.getString("id"));
}
if (jsonObject.has("picture")) {
contributor.setImageUrl(jsonObject.getString("picture"));
}
if (jsonObject.has("given_name")) {
contributor.setFirstName(jsonObject.getString("given_name"));
}
if (jsonObject.has("family_name")) {
contributor.setLastName(jsonObject.getString("family_name"));
}
} catch (JSONException e) {
logger.error(null, e);
}
Contributor existingContributor = contributorDao.read(contributor.getEmail());
if (existingContributor == null) {
// Store new Contributor in database
contributor.setRegistrationTime(Calendar.getInstance());
if (contributor.getEmail().endsWith("@elimu.ai")) {
contributor.setRoles(new HashSet<>(Arrays.asList(Role.ADMIN, Role.ANALYST, Role.CONTRIBUTOR)));
} else {
contributor.setRoles(new HashSet<>(Arrays.asList(Role.CONTRIBUTOR)));
}
contributorDao.create(contributor);
// Send welcome e-mail
String to = contributor.getEmail();
String from = "elimu.ai <info@elimu.ai>";
String subject = "Welcome to the community";
String title = "Welcome!";
String firstName = StringUtils.isBlank(contributor.getFirstName()) ? "" : contributor.getFirstName();
String htmlText = "<p>Hi, " + firstName + "</p>";
htmlText += "<p>Thank you very much for registering as a contributor to the elimu.ai community. We are glad to see you join us!</p>";
htmlText += "<h2>Purpose</h2>";
htmlText += "<p>The purpose of elimu.ai is to provide <i>every child</i> with access to quality basic education.</p>";
htmlText += "<h2>Why?</h2>";
htmlText += "<p>The word \"elimu\" is Swahili for \"education\". We believe that a free quality education is the right of every child no matter her social or geographical background.</p>";
htmlText += "<h2>How?</h2>";
htmlText += "<p>With your help, this is what we aim to achieve:</p>";
htmlText += "<p><blockquote>\"We build tablet-based software that teaches a child to read, write and calculate fully autonomously, without guidance from qualified teachers.\"</blockquote></p>";
htmlText += "<p><img src=\"http://elimu.ai/static/img/banner-en.jpg\" alt=\"\" style=\"width: 564px; max-width: 100%;\" /></p>";
htmlText += "<h2>Chat</h2>";
htmlText += "<p>At http://slack.elimu.ai you can chat with the other community members.</p>";
Mailer.sendHtmlWithButton(to, null, from, subject, title, htmlText, "Open chat", "http://slack.elimu.ai");
if (EnvironmentContextLoaderListener.env == Environment.PROD) {
// Post notification in Slack
String name = "";
if (StringUtils.isNotBlank(contributor.getFirstName())) {
name += "(";
name += contributor.getFirstName();
if (StringUtils.isNotBlank(contributor.getLastName())) {
name += " " + contributor.getLastName();
}
name += ")";
}
String text = URLEncoder.encode("A new contributor " + name + " just joined the community: ") + "http://elimu.ai/content/community/contributors";
String iconUrl = contributor.getImageUrl();
SlackApiHelper.postMessage(null, text, iconUrl, null);
}
} else {
// Update existing contributor with latest values fetched from provider
if (StringUtils.isNotBlank(contributor.getProviderIdGoogle())) {
existingContributor.setProviderIdGoogle(contributor.getProviderIdGoogle());
}
if (StringUtils.isNotBlank(contributor.getImageUrl())) {
existingContributor.setImageUrl(contributor.getImageUrl());
}
// TODO: firstName/lastName
if (StringUtils.isBlank(existingContributor.getReferrer())) {
existingContributor.setReferrer(contributor.getReferrer());
}
if (StringUtils.isBlank(existingContributor.getUtmSource())) {
existingContributor.setUtmSource(contributor.getUtmSource());
}
if (StringUtils.isBlank(existingContributor.getUtmMedium())) {
existingContributor.setUtmMedium(contributor.getUtmMedium());
}
if (StringUtils.isBlank(existingContributor.getUtmCampaign())) {
existingContributor.setUtmCampaign(contributor.getUtmCampaign());
}
if (StringUtils.isBlank(existingContributor.getUtmTerm())) {
existingContributor.setUtmTerm(contributor.getUtmTerm());
}
if (existingContributor.getReferralId() == null) {
existingContributor.setReferralId(contributor.getReferralId());
}
contributorDao.update(existingContributor);
// Contributor registered previously
contributor = existingContributor;
}
// Authenticate
new CustomAuthenticationManager().authenticateUser(contributor);
// Add Contributor object to session
request.getSession().setAttribute("contributor", contributor);
SignOnEvent signOnEvent = new SignOnEvent();
signOnEvent.setContributor(contributor);
signOnEvent.setCalendar(Calendar.getInstance());
signOnEvent.setServerName(request.getServerName());
signOnEvent.setProvider(Provider.GOOGLE);
signOnEvent.setRemoteAddress(request.getRemoteAddr());
signOnEvent.setUserAgent(StringUtils.abbreviate(request.getHeader("User-Agent"), 1000));
signOnEvent.setReferrer(CookieHelper.getReferrer(request));
signOnEvent.setUtmSource(CookieHelper.getUtmSource(request));
signOnEvent.setUtmMedium(CookieHelper.getUtmMedium(request));
signOnEvent.setUtmCampaign(CookieHelper.getUtmCampaign(request));
signOnEvent.setUtmTerm(CookieHelper.getUtmTerm(request));
signOnEvent.setReferralId(CookieHelper.getReferralId(request));
signOnEventDao.create(signOnEvent);
return "redirect:/content";
}
}
Aggregations