Search in sources :

Example 11 with Permission

use of com.willshex.blogwt.shared.api.datatype.Permission in project blogwt by billy1380.

the class DevServlet method doGet.

/* (non-Javadoc)
	 * 
	 * @see com.willshex.service.ContextAwareServlet#doGet() */
@Override
protected void doGet() throws ServletException, IOException {
    super.doGet();
    String action = REQUEST.get().getParameter("action");
    if (action != null) {
        action = action.toLowerCase();
    }
    if ("gentags".equals(action)) {
        TagServiceProvider.provide().generateTags();
    } else if (action != null && action.startsWith("index")) {
        PageServiceProvider.provide();
        PostServiceProvider.provide();
        UserServiceProvider.provide();
        ((ISearch<?>) ServiceDiscovery.getService("blogwt." + action.replace("index", ""))).indexAll();
    } else if ("clearsearch".equals(action)) {
        PersistenceServiceProvider.provide();
        String name = REQUEST.get().getParameter("index");
        String ids = REQUEST.get().getParameter("ids");
        String[] split = ids.split(",");
        for (String id : split) {
            SearchHelper.deleteSearch(name, id);
        }
    } else if ("linkall".equals(action)) {
        PostServiceProvider.provide().linkAll();
    } else if ("clearlinks".equals(action)) {
        PostServiceProvider.provide().clearLinks();
    } else if ("archiveall".equals(action)) {
        ArchiveEntryServiceProvider.provide().generateArchive();
    } else if ("fixroles".equals(action)) {
        Collection<Role> all = RoleHelper.createAll();
        all.stream().forEach(role -> {
            Role loaded = RoleServiceProvider.provide().getCodeRole(role.code);
            if (loaded == null || loaded.id == null) {
                RoleServiceProvider.provide().addRole(role);
            }
            if (role.permissions != null) {
                role.permissions.stream().forEach(i -> {
                    Permission lp = PermissionServiceProvider.provide().getCodePermission(i.code);
                    if (lp == null) {
                        if (LOG.isLoggable(Level.WARNING)) {
                            LOG.warning("Could not find permission with code [" + i.code + "], might want to run [fixpermissions] action");
                        }
                    } else {
                        if (loaded.permissions == null) {
                            loaded.permissions = new ArrayList<>();
                        }
                        loaded.permissions.add(lp);
                    }
                });
                RoleServiceProvider.provide().updateRole(loaded);
            }
        });
    } else if ("fixpermissions".equals(action)) {
        Collection<Permission> all = PermissionHelper.createAll();
        Permission loaded;
        for (Permission permission : all) {
            loaded = PermissionServiceProvider.provide().getCodePermission(permission.code);
            if (loaded == null || loaded.id == null) {
                PermissionServiceProvider.provide().addPermission(permission);
            }
        }
    } else if ("getposts".equals(action)) {
        RESPONSE.get().getOutputStream().print(JsonUtils.beautifyJson((new GetPostsActionHandler()).handle((GetPostsRequest) new GetPostsRequest().showAll(Boolean.TRUE).pager(PagerHelper.createDefaultPager()).accessCode(ApiValidator.DEV_ACCESS_CODE)).toString()));
    } else if ("staticurl".equals(action)) {
        List<Resource> resources = ResourceServiceProvider.provide().getResources(Integer.valueOf(0), Integer.valueOf(Integer.MAX_VALUE), null, null);
        JsonObject object;
        for (Resource resource : resources) {
            if (resource.properties != null) {
                if (resource.properties.contains(":image")) {
                    resource.properties = resource.properties.replace(":image", ":\"image").replace("}", "\"}");
                }
                object = new JsonParser().parse(resource.properties).getAsJsonObject();
            } else {
                object = new JsonObject();
            }
            if (!object.has("staticUrl") || object.get("staticUrl").getAsString().startsWith("http")) {
                try {
                    object.addProperty("staticUrl", ImagesServiceFactory.getImagesService().getServingUrl(ServingUrlOptions.Builder.withBlobKey(new BlobKey(resource.data.replace("gs://", "")))).replaceFirst("https:\\/\\/", "//").replaceFirst("http:\\/\\/", "//"));
                } catch (Throwable e) {
                    if (LOG.isLoggable(Level.FINE)) {
                        LOG.fine("Could not update resource");
                    }
                }
                resource.properties = object.toString();
                ResourceServiceProvider.provide().updateResource(resource);
            }
        }
    } else if ("fixmetanotifications".equals(action)) {
        List<MetaNotification> metas = MetaNotificationHelper.createAll();
        for (MetaNotification meta : metas) {
            if (MetaNotificationServiceProvider.provide().getCodeMetaNotification(meta.code) == null) {
                meta = MetaNotificationServiceProvider.provide().addMetaNotification(meta);
                LOG.info("added meta notification [" + meta.code + "] with id [" + meta.id + "]");
            } else {
                LOG.info("Meta notification [" + meta.code + "] already exists");
            }
        }
    } else if ("admin".equals(action)) {
        User user = UserServiceProvider.provide().getUsernameUser(REQUEST.get().getParameter("user"));
        UserServiceProvider.provide().addUserRolesAndPermissions(user, Arrays.asList(RoleServiceProvider.provide().getCodeRole(RoleHelper.ADMIN)), null);
    } else if ("genandshowdownload".equals(action)) {
        String idParam = REQUEST.get().getParameter("id");
        Long id = Long.valueOf(idParam);
        GeneratedDownload d = GeneratedDownloadServiceProvider.provide().getGeneratedDownload(id);
        Stack stack = Stack.parse(d.parameters);
        Filter filter = Filter.fromStack(stack);
        switch(filter.type) {
            default:
                break;
        }
    }
}
Also used : GetPostsActionHandler(com.willshex.blogwt.server.api.blog.action.GetPostsActionHandler) User(com.willshex.blogwt.shared.api.datatype.User) GetPostsRequest(com.willshex.blogwt.shared.api.blog.call.GetPostsRequest) Resource(com.willshex.blogwt.shared.api.datatype.Resource) JsonObject(com.google.gson.JsonObject) MetaNotification(com.willshex.blogwt.shared.api.datatype.MetaNotification) Stack(com.willshex.blogwt.shared.page.Stack) Role(com.willshex.blogwt.shared.api.datatype.Role) BlobKey(com.google.appengine.api.blobstore.BlobKey) GeneratedDownload(com.willshex.blogwt.shared.api.datatype.GeneratedDownload) Filter(com.willshex.blogwt.shared.page.search.Filter) Permission(com.willshex.blogwt.shared.api.datatype.Permission) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) JsonParser(com.google.gson.JsonParser)

Example 12 with Permission

use of com.willshex.blogwt.shared.api.datatype.Permission in project blogwt by billy1380.

the class CreatePostActionHandler method handle.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
	 * gson.web.service.shared.Request,
	 * com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(CreatePostRequest input, CreatePostResponse output) throws Exception {
    ApiValidator.request(input, CreatePostRequest.class);
    ApiValidator.accessCode(input.accessCode, "input.accessCode");
    output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
    List<Permission> permissions = new ArrayList<Permission>();
    Permission postPermission = PermissionServiceProvider.provide().getCodePermission(PermissionHelper.MANAGE_POSTS);
    permissions.add(postPermission);
    UserValidator.authorisation(input.session.user, permissions, "input.session.user");
    input.post = PostValidator.validate(input.post, "input.post");
    input.post.author = input.session.user;
    if (Boolean.TRUE.equals(input.publish)) {
        input.post.published = new Date();
    }
    input.post.listed = (input.post.listed == null ? Boolean.TRUE : input.post.listed);
    input.post.commentsEnabled = (input.post.commentsEnabled == null ? Boolean.FALSE : input.post.commentsEnabled);
    output.post = PostServiceProvider.provide().addPost(input.post);
}
Also used : Permission(com.willshex.blogwt.shared.api.datatype.Permission) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 13 with Permission

use of com.willshex.blogwt.shared.api.datatype.Permission in project blogwt by billy1380.

the class GetPostsActionHandler method handle.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
	 * gson.web.service.shared.Request,
	 * com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(GetPostsRequest input, GetPostsResponse output) throws Exception {
    ApiValidator.request(input, GetPostsRequest.class);
    ApiValidator.accessCode(input.accessCode, "input.accessCode");
    Boolean showAll = Boolean.TRUE.equals(input.showAll) ? Boolean.TRUE : Boolean.FALSE;
    if (input.session != null) {
        try {
            output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
            List<Permission> permissions = new ArrayList<Permission>();
            Permission postPermission = PermissionServiceProvider.provide().getCodePermission(PermissionHelper.MANAGE_POSTS);
            permissions.add(postPermission);
            try {
                UserValidator.authorisation(input.session.user, permissions, "input.session.user");
            } catch (AuthorisationException aEx) {
                showAll = Boolean.FALSE;
            }
        } catch (InputValidationException ex) {
            output.session = input.session = null;
            showAll = Boolean.FALSE;
        }
    } else {
        showAll = Boolean.FALSE;
    }
    if (!showAll) {
        input.pager.sortBy = PostSortType.PostSortTypePublished.toString();
    }
    if (input.includePostContents == null) {
        input.includePostContents = Boolean.FALSE;
    }
    boolean postsForTag = false, postsForArchiveEntry = false, postsForQuery = false;
    if (input.tag != null && input.tag.length() > 0) {
        postsForTag = true;
        Tag tag = TagServiceProvider.provide().getSlugTag(input.tag);
        if (tag != null) {
            output.posts = PersistenceHelper.batchLookup(PostServiceProvider.provide(), tag.postKeys);
        }
    }
    if (!postsForTag && input.archiveEntry != null) {
        postsForTag = true;
        if (input.archiveEntry.posts != null) {
            output.posts = input.archiveEntry.posts = PostValidator.lookupAll(input.archiveEntry.posts, "input.archiveEntry.posts");
        } else {
            input.archiveEntry = ArchiveEntryValidator.lookup(input.archiveEntry, "input.archiveEntry");
            output.posts = PersistenceHelper.batchLookup(PostServiceProvider.provide(), input.archiveEntry.postKeys);
        }
    }
    if (!postsForTag && !postsForArchiveEntry && input.query != null) {
        postsForQuery = true;
        if (input.session != null && input.session.user != null) {
            output.posts = PostServiceProvider.provide().getUserViewablePartialSlugPosts(input.query, input.session.user, showAll, input.includePostContents, input.pager.start, input.pager.count, PostSortType.fromString(input.pager.sortBy), input.pager.sortDirection);
        } else {
            output.posts = PostServiceProvider.provide().getPartialSlugPosts(input.query, showAll, input.includePostContents, input.pager.start, input.pager.count, PostSortType.PostSortTypePublished, SortDirectionType.SortDirectionTypeDescending);
        }
    }
    if (!postsForTag && !postsForArchiveEntry && !postsForQuery) {
        output.posts = PostServiceProvider.provide().getPosts(showAll, input.includePostContents, input.pager.start, input.pager.count, PostSortType.fromString(input.pager.sortBy), input.pager.sortDirection);
    }
    if (output.posts != null) {
        Map<Key<User>, User> users = new HashMap<Key<User>, User>();
        for (Post post : output.posts) {
            if (users.get(post.authorKey) == null) {
                users.put(post.authorKey, UserHelper.stripSensitive(UserServiceProvider.provide().getUser(keyToId(post.authorKey))));
            }
            post.author = users.get(post.authorKey);
        }
    }
    output.pager = PagerHelper.moveForward(input.pager);
}
Also used : User(com.willshex.blogwt.shared.api.datatype.User) HashMap(java.util.HashMap) Post(com.willshex.blogwt.shared.api.datatype.Post) ArrayList(java.util.ArrayList) Permission(com.willshex.blogwt.shared.api.datatype.Permission) InputValidationException(com.willshex.gson.web.service.server.InputValidationException) Tag(com.willshex.blogwt.shared.api.datatype.Tag) AuthorisationException(com.willshex.blogwt.server.api.exception.AuthorisationException) Key(com.googlecode.objectify.Key)

Example 14 with Permission

use of com.willshex.blogwt.shared.api.datatype.Permission in project blogwt by billy1380.

the class SetupBlogActionHandler method handle.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
	 * gson.web.service.shared.Request,
	 * com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(SetupBlogRequest input, SetupBlogResponse output) throws Exception {
    ApiValidator.request(input, SetupBlogRequest.class);
    IPropertyService propertyService = PropertyServiceProvider.provide();
    if (propertyService.getNamedProperty(PropertyHelper.TITLE) != null)
        ApiValidator.throwServiceError(ServiceException.class, ApiError.ExistingSetup, "input.properties");
    ApiValidator.request(input, SetupBlogRequest.class);
    ApiValidator.accessCode(input.accessCode, "input.accessCode");
    ApiValidator.notNull(input.properties, Property.class, "input.properties");
    ApiValidator.notNull(input.users, User.class, "input.users");
    input.properties = PropertyValidator.setup(input.properties, "input.properties");
    propertyService.addPropertyBatch(input.properties);
    RoleServiceProvider.provide().addRole(RoleHelper.createFull(RoleHelper.ADMIN, RoleHelper.ADMIN_NAME, RoleHelper.ADMIN_DESCRIPTION));
    for (Permission permission : PermissionHelper.createAll()) {
        PermissionServiceProvider.provide().addPermission(permission);
    }
    input.users = UserValidator.validateAll(input.users, "input.users");
    for (User user : input.users) {
        // users are either admins or nothing
        user.roles = RoleValidator.lookupAll(user.roles, "input.users[n].roles");
        // users added at startup are verified
        user.verified = Boolean.TRUE;
    }
    UserServiceProvider.provide().addUserBatch(input.users);
}
Also used : User(com.willshex.blogwt.shared.api.datatype.User) ServiceException(com.willshex.gson.web.service.server.ServiceException) IPropertyService(com.willshex.blogwt.server.service.property.IPropertyService) Permission(com.willshex.blogwt.shared.api.datatype.Permission)

Example 15 with Permission

use of com.willshex.blogwt.shared.api.datatype.Permission in project blogwt by billy1380.

the class GetRolesAndPermissionsActionHandler method handle.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
	 * gson.web.service.shared.Request,
	 * com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(GetRolesAndPermissionsRequest input, GetRolesAndPermissionsResponse output) throws Exception {
    ApiValidator.request(input, GetRolesAndPermissionsRequest.class);
    ApiValidator.accessCode(input.accessCode, "input.accessCode");
    output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
    input.user = UserValidator.lookup(input.user, "input.user");
    if (!DataTypeHelper.<User>same(input.user, input.session.user)) {
        UserValidator.authorisation(input.session.user, Arrays.asList(PermissionServiceProvider.provide().getCodePermission(PermissionHelper.MANAGE_USERS)), "input.session.user");
    }
    boolean idsOnly = Boolean.TRUE.equals(input.idsOnly);
    if (idsOnly) {
        input.user.roles = PersistenceHelper.typeList(Role.class, input.user.roleKeys);
        input.user.permissions = PersistenceHelper.typeList(Permission.class, input.user.permissionKeys);
    } else {
        if ((input.permissionOnly == null && input.rolesOnly == null) || (Boolean.FALSE.equals(input.rolesOnly) && Boolean.FALSE.equals(input.permissionOnly))) {
            UserHelper.populateRolesAndPermissionsFromKeys(input.user);
            output.roles = input.user.roles;
            output.permissions = input.user.permissions;
        } else if (Boolean.TRUE.equals(input.permissionOnly) && Boolean.TRUE.equals(input.rolesOnly)) {
        } else if (Boolean.TRUE.equals(input.permissionOnly)) {
            UserHelper.populatePermissionsFromKeys(input.user);
            output.permissions = input.user.permissions;
        } else if (Boolean.TRUE.equals(input.rolesOnly)) {
            UserHelper.populateRolesFromKeys(input.user);
            output.roles = input.user.roles;
        }
    }
    if (input.user.roleKeys != null && !Boolean.TRUE.equals(input.rolesOnly) && Boolean.TRUE.equals(input.expandRoles)) {
        List<Permission> expandedPermissions;
        if (idsOnly) {
            Role lookupRole;
            for (Role role : input.user.roles) {
                lookupRole = RoleServiceProvider.provide().getRole(role.id);
                if (lookupRole != null) {
                    expandedPermissions = PersistenceHelper.typeList(Permission.class, lookupRole.permissionKeys);
                    if (expandedPermissions != null) {
                        if (output.permissions != null) {
                            output.permissions.addAll(expandedPermissions);
                        } else {
                            output.permissions = expandedPermissions;
                        }
                    }
                }
            }
        } else {
            for (Role role : input.user.roles) {
                expandedPermissions = PermissionServiceProvider.provide().getRolePermissions(role);
                if (expandedPermissions != null) {
                    if (output.permissions != null) {
                        output.permissions.addAll(expandedPermissions);
                    } else {
                        output.permissions = expandedPermissions;
                    }
                }
            }
        }
    }
}
Also used : Role(com.willshex.blogwt.shared.api.datatype.Role) Permission(com.willshex.blogwt.shared.api.datatype.Permission)

Aggregations

Permission (com.willshex.blogwt.shared.api.datatype.Permission)18 Role (com.willshex.blogwt.shared.api.datatype.Role)8 User (com.willshex.blogwt.shared.api.datatype.User)5 ArrayList (java.util.ArrayList)5 JsonElement (com.google.gson.JsonElement)4 Date (java.util.Date)4 InputValidationException (com.willshex.gson.web.service.server.InputValidationException)3 Key (com.googlecode.objectify.Key)2 AuthorisationException (com.willshex.blogwt.server.api.exception.AuthorisationException)2 BlobKey (com.google.appengine.api.blobstore.BlobKey)1 Document (com.google.appengine.api.search.Document)1 ScoredDocument (com.google.appengine.api.search.ScoredDocument)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 GetPostsActionHandler (com.willshex.blogwt.server.api.blog.action.GetPostsActionHandler)1 IPropertyService (com.willshex.blogwt.server.service.property.IPropertyService)1 Pager (com.willshex.blogwt.shared.api.Pager)1 GetPostsRequest (com.willshex.blogwt.shared.api.blog.call.GetPostsRequest)1 GeneratedDownload (com.willshex.blogwt.shared.api.datatype.GeneratedDownload)1 MetaNotification (com.willshex.blogwt.shared.api.datatype.MetaNotification)1