use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class ApiController method updateUser.
@PatchMapping("/users/{id}")
public Profile updateUser(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
Map<String, Object> entity = readEntity(req);
if (entity.isEmpty()) {
badReq("Missing request body.");
}
String name = (String) entity.get("name");
String location = (String) entity.get("location");
String latlng = (String) entity.get("latlng");
String website = (String) entity.get("website");
String aboutme = (String) entity.get("aboutme");
String picture = (String) entity.get("picture");
Model model = new ExtendedModelMap();
profileController.edit(id, name, location, latlng, website, aboutme, picture, req, model);
Profile profile = (Profile) model.getAttribute("user");
if (profile == null) {
res.setStatus(HttpStatus.NOT_FOUND.value());
return null;
}
if (entity.containsKey("spaces")) {
profile.setSpaces(new HashSet<>(readSpaces(((List<String>) entity.getOrDefault("spaces", Collections.emptyList())).toArray(new String[0]))));
pc.update(profile);
}
return profile;
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class ApiController method createPost.
@PostMapping("/posts")
public Map<String, Object> createPost(HttpServletRequest req, HttpServletResponse res) {
Map<String, Object> entity = readEntity(req);
if (!entity.containsKey(Config._TYPE)) {
entity.put(Config._TYPE, POST_TYPES[0]);
} else if (!StringUtils.equalsAnyIgnoreCase((CharSequence) entity.get(Config._TYPE), POST_TYPES)) {
badReq("Invalid post type - could be one of " + Arrays.toString(POST_TYPES));
}
Post post = ParaObjectUtils.setAnnotatedFields(entity);
if (!StringUtils.isBlank(post.getCreatorid())) {
Profile authUser = pc.read(Profile.id(post.getCreatorid()));
if (authUser != null) {
req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
}
}
Model model = new ExtendedModelMap();
List<String> spaces = readSpaces(post.getSpace());
post.setSpace(spaces.iterator().hasNext() ? spaces.iterator().next() : null);
if (post.isQuestion()) {
questionsController.post(post.getLocation(), post.getLatlng(), post.getAddress(), post.getSpace(), req, res, model);
} else if (post.isReply()) {
questionController.reply(post.getParentid(), "", null, req, res, model);
} else {
badReq("Invalid post type - could be one of " + Arrays.toString(POST_TYPES));
}
checkForErrorsAndThrow(model);
Map<String, Object> newpost = (Map<String, Object>) model.getAttribute("newpost");
res.setStatus(HttpStatus.CREATED.value());
return newpost;
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class ApiController method createReport.
@PostMapping("/reports")
public Report createReport(HttpServletRequest req, HttpServletResponse res) {
Map<String, Object> entity = readEntity(req);
if (entity.isEmpty()) {
badReq("Missing request body.");
}
String creatorid = (String) entity.get(Config._CREATORID);
if (!StringUtils.isBlank(creatorid)) {
Profile authUser = pc.read(Profile.id(creatorid));
if (authUser != null) {
req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
}
}
Model model = new ExtendedModelMap();
reportsController.create(req, res, model);
checkForErrorsAndThrow(model);
Report newreport = (Report) model.getAttribute("newreport");
res.setStatus(HttpStatus.CREATED.value());
return newreport;
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class ApiController method createUser.
@PostMapping("/users")
public Map<String, Object> createUser(HttpServletRequest req, HttpServletResponse res) {
Map<String, Object> entity = readEntity(req);
if (entity.isEmpty()) {
badReq("Missing request body.");
}
Map<String, Object> userEntity = new HashMap<>();
userEntity.put(Config._TYPE, Utils.type(User.class));
userEntity.put(Config._NAME, entity.get(Config._NAME));
userEntity.put(Config._EMAIL, entity.get(Config._EMAIL));
userEntity.put(Config._IDENTIFIER, entity.get(Config._IDENTIFIER));
userEntity.put(Config._GROUPS, entity.get(Config._GROUPS));
userEntity.put("active", entity.getOrDefault("active", true));
userEntity.put("picture", entity.get("picture"));
User newUser = ParaObjectUtils.setAnnotatedFields(new User(), userEntity, null);
newUser.setPassword((String) entity.get(Config._PASSWORD));
newUser.setIdentifier(StringUtils.isBlank(newUser.getIdentifier()) ? newUser.getEmail() : newUser.getIdentifier());
String[] errors = ValidationUtils.validateObject(newUser);
if (errors.length == 0) {
// generic and password providers are identical but this was fixed in Para 1.37.1 (backwards compatibility)
String provider = "generic".equals(newUser.getIdentityProvider()) ? "password" : newUser.getIdentityProvider();
User createdUser = pc.signIn(provider, newUser.getIdentifier() + Config.SEPARATOR + newUser.getName() + Config.SEPARATOR + newUser.getPassword(), false);
// user is probably active:false so activate them
List<User> created = pc.findQuery(newUser.getType(), Config._EMAIL + ":" + newUser.getEmail());
if (createdUser == null && !created.isEmpty()) {
createdUser = created.iterator().next();
if (Utils.timestamp() - createdUser.getTimestamp() > TimeUnit.SECONDS.toMillis(20)) {
// user existed previously
createdUser = null;
} else if (newUser.getActive() && !createdUser.getActive()) {
createdUser.setActive(true);
pc.update(createdUser);
}
}
if (createdUser == null) {
badReq("Failed to create user. User may already exist.");
} else {
Profile profile = Profile.fromUser(createdUser);
profile.getSpaces().addAll(readSpaces(((List<String>) entity.getOrDefault("spaces", Collections.emptyList())).toArray(new String[0])));
res.setStatus(HttpStatus.CREATED.value());
pc.create(profile);
Map<String, Object> payload = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(profile, false));
payload.put("user", createdUser);
utils.triggerHookEvent("user.signup", payload);
logger.info("Created new user through API '{}' with id={}, groups={}, spaces={}.", createdUser.getName(), profile.getId(), profile.getGroups(), profile.getSpaces());
Map<String, Object> result = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(profile, false));
result.put("user", createdUser);
return result;
}
}
badReq("Failed to create user - " + String.join("; ", errors));
return null;
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class AdminController method addSpace.
@PostMapping("/add-space")
public String addSpace(@RequestParam String space, HttpServletRequest req, HttpServletResponse res, Model model) {
Profile authUser = utils.getAuthUser(req);
if (!StringUtils.isBlank(space) && utils.isAdmin(authUser)) {
Sysprop spaceObj = utils.buildSpaceObject(space);
if (utils.isDefaultSpace(spaceObj.getId()) || pc.getCount("scooldspace") >= MAX_SPACES || pc.read(spaceObj.getId()) != null) {
if (utils.isAjaxRequest(req)) {
res.setStatus(400);
return "space";
} else {
return "redirect:" + ADMINLINK + "?code=7&error=true";
}
} else {
if (pc.create(spaceObj) != null) {
authUser.getSpaces().add(spaceObj.getId() + Config.SEPARATOR + spaceObj.getName());
authUser.update();
model.addAttribute("space", spaceObj);
utils.getAllSpaces().add(spaceObj);
} else {
model.addAttribute("error", Collections.singletonMap("name", utils.getLang(req).get("posts.error1")));
}
}
} else {
model.addAttribute("error", Collections.singletonMap("name", utils.getLang(req).get("requiredfield")));
}
if (utils.isAjaxRequest(req)) {
res.setStatus(model.containsAttribute("error") ? 400 : 200);
return "space";
} else {
return "redirect:" + ADMINLINK;
}
}
Aggregations