Search in sources :

Example 46 with PermissionHolder

use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.

the class AbstractPublishedPointModelMapping method map.

@Override
public MODEL map(Object from, PermissionHolder user, RestModelMapper mapper) {
    VO vo = (VO) from;
    MODEL model = null;
    try {
        model = toClass().getDeclaredConstructor().newInstance();
        setModelProperties(model, vo, user, mapper);
        return model;
    } catch (Exception e) {
        throw new RuntimeException("Failed to create model", e);
    }
}
Also used : PublishedPointVO(com.serotonin.m2m2.vo.publish.PublishedPointVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException)

Example 47 with PermissionHolder

use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.

the class TestingRestController method insertPersistentSession.

@ApiOperation(value = "Create a persistent session entry")
@RequestMapping(method = RequestMethod.POST, value = { "/persistent-session" })
public ResponseEntity<MangoSessionDataModel> insertPersistentSession(@RequestBody MangoSessionDataModel model, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request) {
    // Fill in some helpful pieces if they are missing
    if (model.getContextPath() == null) {
        model.setContextPath(sessionDataStore.getSessionContext().getCanonicalContextPath());
    }
    if (model.getVirtualHost() == null) {
        model.setVirtualHost(sessionDataStore.getSessionContext().getVhost());
    }
    HttpSession session = request.getSession(false);
    if (model.getLastAccessTime() == null) {
        model.setLastAccessTime(new Date(session.getLastAccessedTime()));
    }
    if (model.getCreateTime() == null) {
        model.setCreateTime(new Date(session.getCreationTime()));
    }
    sessionDataStore.add(modelMapper.unMap(model, MangoSessionDataVO.class, user));
    return new ResponseEntity<>(model, HttpStatus.CREATED);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpSession(javax.servlet.http.HttpSession) MangoSessionDataVO(com.serotonin.m2m2.vo.MangoSessionDataVO) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with PermissionHolder

use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.

the class TranslationsController method publicNamespacedTranslations.

@ApiOperation(value = "Get translations for public namespaces", notes = "Namespace must be base , ie public not public.messages. Returns sub-namespaces too. For > 1 use comma common,public")
@RequestMapping(method = RequestMethod.GET, value = "/public/{namespaces}")
@AnonymousAccess
public TranslationsModel publicNamespacedTranslations(@ApiParam(value = "Message Namespaces, simmilar to java package structure", allowMultiple = true) @PathVariable String[] namespaces, @ApiParam(value = "Language for translation (must have language pack installed)", allowMultiple = false) @RequestParam(value = "language", required = false) String language, @ApiParam(value = "Use server language for translation", allowMultiple = false) @RequestParam(value = "server", required = false, defaultValue = "false") boolean server, @RequestParam(value = "browser", required = false, defaultValue = "false") boolean browser, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request) {
    // Confirm the requested namespace is indeed public
    for (String namespace : namespaces) {
        if (!this.publicNamespaces.contains(namespace)) {
            throw new BadRequestException();
        }
    }
    TranslationsModel resultMap = new TranslationsModel();
    Locale locale = this.getLocale(language, server, browser, request, user);
    resultMap.setLocale(locale.toLanguageTag());
    resultMap.setTranslations(getTranslationMap(namespaces, locale));
    resultMap.setNamespaces(namespaces);
    return resultMap;
}
Also used : Locale(java.util.Locale) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) AnonymousAccess(com.serotonin.m2m2.web.mvc.spring.security.permissions.AnonymousAccess) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 49 with PermissionHolder

use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.

the class UserRestController method updateUser.

@ApiOperation(value = "Update User", notes = "Admin or Update Self only", response = UserModel.class)
@RequestMapping(method = RequestMethod.PUT, value = "/{username}")
public ResponseEntity<UserModel> updateUser(@PathVariable String username, @ApiParam(value = "User", required = true) @RequestBody UserModel model, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request, UriComponentsBuilder builder, Authentication authentication) {
    User existing = service.get(username);
    User currentUser = user.getUser();
    if (currentUser != null && existing.getId() == currentUser.getId() && !(authentication instanceof UsernamePasswordAuthenticationToken))
        throw new PermissionException(new TranslatableMessage("rest.error.usernamePasswordOnly"), user);
    User update = service.update(existing.getId(), model.toVO());
    sessionRegistry.userUpdated(request, update);
    URI location = builder.path("/users/{username}").buildAndExpand(update.getUsername()).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(new UserModel(update), headers, HttpStatus.OK);
}
Also used : PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) UserModel(com.infiniteautomation.mango.rest.latest.model.user.UserModel) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) User(com.serotonin.m2m2.vo.User) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 50 with PermissionHolder

use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.

the class UserRestController method exportQuery.

@ApiOperation(value = "Export formatted for Configuration Import by supplying an RQL query", notes = "User must have read permission")
@RequestMapping(method = RequestMethod.GET, value = "/export", produces = MediaTypes.SEROTONIN_JSON_VALUE)
public Map<String, JsonStreamedArray> exportQuery(HttpServletRequest request, @AuthenticationPrincipal PermissionHolder user) {
    ASTNode rql = RQLUtils.parseRQLtoAST(request.getQueryString());
    Map<String, JsonStreamedArray> export = new HashMap<>();
    if (!permissionService.hasAdminRole(user)) {
        User currentUser = user.getUser();
        rql = RQLUtils.addAndRestriction(rql, new ASTNode("eq", "id", currentUser == null ? Common.NEW_ID : currentUser.getId()));
    }
    export.put("users", new StreamedSeroJsonVORqlQuery<>(service, rql, null, null, null));
    return export;
}
Also used : User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) ASTNode(net.jazdw.rql.parser.ASTNode) JsonStreamedArray(com.serotonin.json.type.JsonStreamedArray) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)120 ApiOperation (io.swagger.annotations.ApiOperation)97 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)97 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)64 ResponseEntity (org.springframework.http.ResponseEntity)53 HttpHeaders (org.springframework.http.HttpHeaders)50 URI (java.net.URI)48 ArrayList (java.util.ArrayList)37 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)34 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)29 List (java.util.List)27 User (com.serotonin.m2m2.vo.User)25 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)24 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)24 BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)19 HashMap (java.util.HashMap)19 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)18 Common (com.serotonin.m2m2.Common)18 Collectors (java.util.stream.Collectors)17 Role (com.serotonin.m2m2.vo.role.Role)16