Search in sources :

Example 81 with Profile

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;
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Model(org.springframework.ui.Model) ParaObject(com.erudika.para.core.ParaObject) Profile(com.erudika.scoold.core.Profile) PatchMapping(org.springframework.web.bind.annotation.PatchMapping)

Example 82 with 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;
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Post(com.erudika.scoold.core.Post) Model(org.springframework.ui.Model) ParaObject(com.erudika.para.core.ParaObject) Map(java.util.Map) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 83 with Profile

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;
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Report(com.erudika.scoold.core.Report) Model(org.springframework.ui.Model) ParaObject(com.erudika.para.core.ParaObject) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 84 with Profile

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;
}
Also used : User(com.erudika.para.core.User) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParaObject(com.erudika.para.core.ParaObject) List(java.util.List) Profile(com.erudika.scoold.core.Profile) LinkedHashMap(java.util.LinkedHashMap) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 85 with Profile

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;
    }
}
Also used : Sysprop(com.erudika.para.core.Sysprop) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

Profile (com.erudika.scoold.core.Profile)85 PostMapping (org.springframework.web.bind.annotation.PostMapping)47 Post (com.erudika.scoold.core.Post)29 ParaObject (com.erudika.para.core.ParaObject)25 User (com.erudika.para.core.User)19 Pager (com.erudika.para.core.utils.Pager)19 HashMap (java.util.HashMap)15 LinkedHashMap (java.util.LinkedHashMap)15 Question (com.erudika.scoold.core.Question)13 Reply (com.erudika.scoold.core.Reply)13 Report (com.erudika.scoold.core.Report)11 IOException (java.io.IOException)11 List (java.util.List)11 Map (java.util.Map)11 UnapprovedReply (com.erudika.scoold.core.UnapprovedReply)10 GetMapping (org.springframework.web.bind.annotation.GetMapping)10 Sysprop (com.erudika.para.core.Sysprop)9 Config (com.erudika.para.core.utils.Config)9 Utils (com.erudika.para.core.utils.Utils)9 Comment (com.erudika.scoold.core.Comment)9