Search in sources :

Example 71 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class GravatarAvatarGeneratorTest method getRawLink_should_use_email_of_profile.

@Test
public void getRawLink_should_use_email_of_profile() {
    String email = "toto@example.com";
    Profile profile = getProfileWithEmail(email);
    AvatarFormat format = AvatarFormat.Square32;
    String urlOfEmail = generator.getRawLink(email);
    String urlOfProfile = generator.getRawLink(profile);
    assertEquals(urlOfEmail, urlOfProfile);
}
Also used : Profile(com.erudika.scoold.core.Profile) Test(org.junit.Test)

Example 72 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class ImgurAvatarRepositoryTest method setUp.

@Before
public void setUp() {
    this.profile = new Profile();
    this.profile.setUser(new User());
    this.defaultRepository = new DefaultAvatarRepository();
    this.repository = new ImgurAvatarRepository(defaultRepository);
}
Also used : User(com.erudika.para.core.User) Profile(com.erudika.scoold.core.Profile) Before(org.junit.Before)

Example 73 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class AdminController method backup.

@GetMapping(value = "/export", produces = "application/zip")
public ResponseEntity<StreamingResponseBody> backup(HttpServletRequest req, HttpServletResponse response) {
    Profile authUser = utils.getAuthUser(req);
    if (!utils.isAdmin(authUser)) {
        return new ResponseEntity<StreamingResponseBody>(HttpStatus.UNAUTHORIZED);
    }
    String fileName = App.identifier(Config.getConfigParam("access_key", "scoold")) + "_" + Utils.formatDate("YYYYMMdd_HHmmss", Locale.US);
    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".zip");
    return new ResponseEntity<StreamingResponseBody>(out -> {
        // export all fields, even those which are JSON-ignored
        ObjectWriter writer = JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build().writer().without(SerializationFeature.INDENT_OUTPUT).without(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
        try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
            long count = 0;
            int partNum = 0;
            // find all objects even if there are more than 10000 users in the system
            Pager pager = new Pager(1, "_docid", false, Config.MAX_ITEMS_PER_PAGE);
            List<ParaObject> objects;
            do {
                objects = pc.findQuery("", "*", pager);
                ZipEntry zipEntry = new ZipEntry(fileName + "_part" + (++partNum) + ".json");
                zipOut.putNextEntry(zipEntry);
                writer.writeValue(zipOut, objects);
                count += objects.size();
            } while (!objects.isEmpty());
            logger.info("Exported {} objects to {}. Downloaded by {} (pager.count={})", count, fileName + ".zip", authUser.getCreatorid() + " " + authUser.getName(), pager.getCount());
        } catch (final IOException e) {
            logger.error("Failed to export data.", e);
        }
    }, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ZipOutputStream(java.util.zip.ZipOutputStream) ParaObject(com.erudika.para.core.ParaObject) Pager(com.erudika.para.core.utils.Pager) ZipEntry(java.util.zip.ZipEntry) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) IOException(java.io.IOException) Profile(com.erudika.scoold.core.Profile) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 74 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class AdminController method generateAPIKey.

@ResponseBody
@PostMapping(path = "/generate-api-key", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Object>> generateAPIKey(@RequestParam Integer validityHours, HttpServletRequest req, Model model) throws ParseException {
    Profile authUser = utils.getAuthUser(req);
    if (utils.isAdmin(authUser)) {
        String jti = UUID.randomUUID().toString();
        Map<String, Object> claims = Collections.singletonMap("jti", jti);
        SignedJWT jwt = utils.generateJWToken(claims, TimeUnit.HOURS.toSeconds(validityHours));
        if (jwt != null) {
            String jwtString = jwt.serialize();
            Date exp = jwt.getJWTClaimsSet().getExpirationTime();
            utils.registerApiKey(jti, jwtString);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("jti", jti);
            data.put("jwt", jwtString);
            data.put("exp", exp == null ? 0L : Utils.formatDate(exp.getTime(), "YYYY-MM-dd HH:mm", Locale.UK));
            return ResponseEntity.ok().body(data);
        }
    }
    return ResponseEntity.status(403).build();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParaObject(com.erudika.para.core.ParaObject) SignedJWT(com.nimbusds.jwt.SignedJWT) Profile(com.erudika.scoold.core.Profile) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 75 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class AdminController method createWebhook.

@PostMapping("/create-webhook")
public String createWebhook(@RequestParam String targetUrl, @RequestParam(required = false) String type, @RequestParam Boolean json, @RequestParam Set<String> events, HttpServletRequest req, Model model) {
    Profile authUser = utils.getAuthUser(req);
    if (Utils.isValidURL(targetUrl) && utils.isAdmin(authUser) && utils.isWebhooksEnabled()) {
        Webhook webhook = new Webhook(targetUrl);
        webhook.setCreate(events.contains("create"));
        webhook.setUpdate(events.contains("update"));
        webhook.setDelete(events.contains("delete"));
        webhook.setCreateAll(events.contains("createAll"));
        webhook.setUpdateAll(events.contains("updateAll"));
        webhook.setDeleteAll(events.contains("deleteAll"));
        webhook.setCustomEvents(events.stream().filter(e -> !StringUtils.equalsAny(e, "create", "update", "delete", "createAll", "updateAll", "deleteAll")).collect(Collectors.toList()));
        if (utils.getCoreScooldTypes().contains(type)) {
            webhook.setTypeFilter(type);
        }
        webhook.setUrlEncoded(!json);
        webhook.resetSecret();
        pc.create(webhook);
    } else {
        model.addAttribute("error", Collections.singletonMap("targetUrl", utils.getLang(req).get("requiredfield")));
        return "base";
    }
    return "redirect:" + ADMINLINK;
}
Also used : Webhook(com.erudika.para.core.Webhook) 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