use of org.olat.core.id.User in project OpenOLAT by OpenOLAT.
the class BGMailHelper method createMailTemplate.
/**
* Internal helper - does all the magic
*
* @param group
* @param actor
* @param subjectKey
* @param bodyKey
* @return
*/
private static MailTemplate createMailTemplate(BusinessGroupShort group, Identity actor, String subjectKey, String bodyKey) {
// get some data about the actor and fetch the translated subject / body via i18n module
String[] bodyArgs = null;
String lang = null;
if (actor != null) {
lang = actor.getUser().getPreferences().getLanguage();
}
Locale locale = I18nManager.getInstance().getLocaleOrDefault(lang);
if (actor != null) {
bodyArgs = new String[] { actor.getUser().getProperty(UserConstants.FIRSTNAME, null), actor.getUser().getProperty(UserConstants.LASTNAME, null), UserManager.getInstance().getUserDisplayEmail(actor, locale), // 2x for compatibility with old i18m properties
UserManager.getInstance().getUserDisplayEmail(actor, locale) };
}
Translator trans = Util.createPackageTranslator(BGMailHelper.class, locale, Util.createPackageTranslator(BusinessGroupListController.class, locale));
String subject = trans.translate(subjectKey);
String body = trans.translate(bodyKey, bodyArgs);
// build learning resources as list of url as string
final BGMailTemplateInfos infos;
if (group != null) {
BusinessGroupService businessGroupService = CoreSpringFactory.getImpl(BusinessGroupService.class);
List<RepositoryEntryShort> repoEntries = businessGroupService.findShortRepositoryEntries(Collections.singletonList(group), 0, -1);
infos = getTemplateInfos(group, repoEntries);
subject = subject.replace("$groupname", infos.getGroupName());
body = body.replace("$groupname", infos.getGroupNameWithUrl());
body = body.replace("$groupdescription", infos.getGroupDescription());
if (StringHelper.containsNonWhitespace(infos.getCourseList())) {
body = body.replace("$courselist", infos.getCourseList());
} else {
body = body.replace("$courselist", trans.translate("notification.mail.no.ressource", null));
}
} else {
infos = new BGMailTemplateInfos("", "", "", "");
}
// create a mail template which all these data
MailTemplate mailTempl = new MailTemplate(subject, body, null) {
@Override
public void putVariablesInMailContext(VelocityContext context, Identity identity) {
// Put user variables into velocity context
User user = identity.getUser();
context.put("firstname", user.getProperty(UserConstants.FIRSTNAME, null));
context.put("lastname", user.getProperty(UserConstants.LASTNAME, null));
// the email of the user, needs to stay named 'login'
context.put("login", user.getProperty(UserConstants.EMAIL, null));
// Put variables from greater context
context.put("groupname", infos.getGroupNameWithUrl());
context.put("groupdescription", infos.getGroupDescription());
if (StringHelper.containsNonWhitespace(infos.getCourseList())) {
context.put("courselist", infos.getCourseList());
} else {
context.put("courselist", trans.translate("notification.mail.no.ressource", null));
}
context.put("courselistempty", trans.translate("notification.mail.no.ressource", null));
}
};
return mailTempl;
}
use of org.olat.core.id.User in project OpenOLAT by OpenOLAT.
the class RegistrationController method createNewUserAfterRegistration.
/**
* OO-92
* this will finally create the user, set all it's userproperties
*
* @return User the newly created, persisted User Object
*/
private Identity createNewUserAfterRegistration() {
// create user with mandatory fields from registration-form
UserManager um = UserManager.getInstance();
User volatileUser = um.createUser(registrationForm.getFirstName(), registrationForm.getLastName(), tempKey.getEmailAddress());
// set user configured language
Preferences preferences = volatileUser.getPreferences();
preferences.setLanguage(registrationForm.getLangKey());
volatileUser.setPreferences(preferences);
// create an identity with the given username / pwd and the user object
String login = registrationForm.getLogin();
String pwd = registrationForm.getPassword();
Identity persistedIdentity = registrationManager.createNewUserAndIdentityFromTemporaryKey(login, pwd, volatileUser, tempKey);
if (persistedIdentity == null) {
showError("user.notregistered");
return null;
} else {
// update other user properties from form
List<UserPropertyHandler> userPropertyHandlers = um.getUserPropertyHandlersFor(RegistrationForm2.USERPROPERTIES_FORM_IDENTIFIER, false);
User persistedUser = persistedIdentity.getUser();
// add eventually static value
UserPropertiesConfig userPropertiesConfig = CoreSpringFactory.getImpl(UserPropertiesConfig.class);
if (registrationModule.isStaticPropertyMappingEnabled()) {
String propertyName = registrationModule.getStaticPropertyMappingName();
String propertyValue = registrationModule.getStaticPropertyMappingValue();
if (StringHelper.containsNonWhitespace(propertyName) && StringHelper.containsNonWhitespace(propertyValue) && userPropertiesConfig.getPropertyHandler(propertyName) != null) {
try {
persistedUser.setProperty(propertyName, propertyValue);
} catch (Exception e) {
logError("Cannot set the static property value", e);
}
}
}
for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
FormItem fi = registrationForm.getPropFormItem(userPropertyHandler.getName());
userPropertyHandler.updateUserFromFormItem(persistedUser, fi);
}
// persist changes in db
um.updateUserFromIdentity(persistedIdentity);
// send notification mail to sys admin
String notiEmail = CoreSpringFactory.getImpl(RegistrationModule.class).getRegistrationNotificationEmail();
if (notiEmail != null) {
registrationManager.sendNewUserNotificationMessage(notiEmail, persistedIdentity);
}
// tell system that this user did accept the disclaimer
registrationManager.setHasConfirmedDislaimer(persistedIdentity);
return persistedIdentity;
}
}
use of org.olat.core.id.User in project OpenOLAT by OpenOLAT.
the class BaseSecurityManagerTest method testCreateIdentity.
@Test
public void testCreateIdentity() {
String username = "createid-" + UUID.randomUUID().toString();
User user = userManager.createUser("first" + username, "last" + username, username + "@frentix.com");
Identity identity = securityManager.createAndPersistIdentityAndUser(username, null, user, BaseSecurityModule.getDefaultAuthProviderIdentifier(), username, "secret");
dbInstance.commitAndCloseSession();
Assert.assertNotNull(identity);
Assert.assertNotNull(identity.getKey());
Assert.assertEquals(username, identity.getName());
Assert.assertNotNull(identity.getUser());
Assert.assertEquals(user, identity.getUser());
Assert.assertEquals("first" + username, identity.getUser().getFirstName());
Assert.assertEquals("last" + username, identity.getUser().getLastName());
Assert.assertEquals("first" + username, identity.getUser().getProperty(UserConstants.FIRSTNAME, null));
Assert.assertEquals("last" + username, identity.getUser().getProperty(UserConstants.LASTNAME, null));
Assert.assertEquals(username + "@frentix.com", identity.getUser().getProperty(UserConstants.EMAIL, null));
}
use of org.olat.core.id.User in project OpenOLAT by OpenOLAT.
the class UserWebService method create.
/**
* Creates and persists a new user entity
* @response.representation.qname {http://www.example.com}userVO
* @response.representation.mediaType application/xml, application/json
* @response.representation.doc The user to persist
* @response.representation.example {@link org.olat.user.restapi.Examples#SAMPLE_USERVO}
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The persisted user
* @response.representation.200.example {@link org.olat.user.restapi.Examples#SAMPLE_USERVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.406.mediaType application/xml, application/json
* @response.representation.406.doc The list of errors
* @response.representation.406.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_ERRORVOes}
* @param user The user to persist
* @param request The HTTP request
* @return the new persisted <code>User</code>
*/
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response create(UserVO user, @Context HttpServletRequest request) {
if (!isUserManager(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
// Check if login is still available
Identity identity = BaseSecurityManager.getInstance().findIdentityByName(user.getLogin());
if (identity != null) {
Locale locale = getLocale(request);
Translator translator = Util.createPackageTranslator(UserShortDescription.class, locale);
String translation = translator.translate("new.error.loginname.choosen");
ErrorVO[] errorVos = new ErrorVO[] { new ErrorVO("org.olat.admin.user", "new.error.loginname.choosen", translation) };
return Response.ok(errorVos).status(Status.NOT_ACCEPTABLE).build();
}
List<ErrorVO> errors = validateUser(null, user, request);
if (errors.isEmpty()) {
User newUser = UserManager.getInstance().createUser(user.getFirstName(), user.getLastName(), user.getEmail());
Identity id = BaseSecurityManager.getInstance().createAndPersistIdentityAndUserWithDefaultProviderAndUserGroup(user.getLogin(), user.getExternalId(), user.getPassword(), newUser);
post(newUser, user, getLocale(request));
UserManager.getInstance().updateUser(newUser);
return Response.ok(get(id)).build();
}
// content not ok
ErrorVO[] errorVos = new ErrorVO[errors.size()];
errors.toArray(errorVos);
return Response.ok(errorVos).status(Status.NOT_ACCEPTABLE).build();
}
use of org.olat.core.id.User in project openolat by klemens.
the class NotificationSubscriptionAndNewsController method event.
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
* org.olat.core.gui.components.Component,
* org.olat.core.gui.control.Event)
*/
@Override
protected void event(UserRequest ureq, Component source, Event event) {
if (source == tabbedPane) {
if (event instanceof TabbedPaneChangedEvent) {
TabbedPaneChangedEvent tabbedEvent = (TabbedPaneChangedEvent) event;
// user clicks the tab the first time
if (tabbedEvent.getNewComponent() == subscriptionPanel && subscriptionCtr == null) {
subscriptionCtr = new NotificationSubscriptionController(ureq, getWindowControl(), subscriberIdentity, false);
listenTo(subscriptionCtr);
subscriptionPanel.setContent(subscriptionCtr.getInitialComponent());
} else // user clicks the tab the first time
if (tabbedEvent.getNewComponent() == rssPanel && rssPanel.getContent() == null) {
VelocityContainer notificationsRssVC = createVelocityContainer("notificationsRSS");
String rssLink = PersonalRSSUtil.getPersonalRssLink(ureq);
notificationsRssVC.contextPut("rssLink", rssLink);
User user = subscriberIdentity.getUser();
String fullName = user.getProperty(UserConstants.FIRSTNAME, getLocale()) + " " + user.getProperty(UserConstants.LASTNAME, getLocale());
notificationsRssVC.contextPut("fullName", fullName);
rssPanel.setContent(notificationsRssVC);
}
// fxdiff BAKS-7 Resume function
tabbedPane.addToHistory(ureq, getWindowControl());
}
}
}
Aggregations