Search in sources :

Example 6 with Role

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

the class RegisterUserActionHandler 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(RegisterUserRequest input, RegisterUserResponse output) throws Exception {
    ApiValidator.request(input, RegisterUserRequest.class);
    ApiValidator.accessCode(input.accessCode, "input.accessCode");
    if (input.session != null) {
        try {
            output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
            UserValidator.authorisation(input.session.user, Arrays.asList(PermissionServiceProvider.provide().getCodePermission(PermissionHelper.MANAGE_USERS)), "input.session.user");
        } catch (InputValidationException ex) {
            output.session = input.session = null;
        }
    } else {
        PropertyValidator.ensureTrue(PropertyHelper.ALLOW_USER_REGISTRATION);
    }
    input.user = UserValidator.validate(input.user, "input.user");
    List<String> codes;
    List<Permission> permissions = null;
    Property property = PropertyServiceProvider.provide().getNamedProperty(PropertyHelper.NEW_USER_PERMISSIONS);
    if (!PropertyHelper.isEmpty(property) && !PropertyHelper.NONE_VALUE.equals(property.value)) {
        codes = TagHelper.convertToTagList(property.value, true);
        for (String code : codes) {
            if (permissions == null) {
                permissions = new ArrayList<Permission>();
            }
            permissions.add(new Permission().code(code.toUpperCase()));
        }
        permissions = PermissionValidator.lookupAll(permissions, PropertyHelper.NEW_USER_PERMISSIONS);
    }
    List<Role> roles = null;
    property = PropertyServiceProvider.provide().getNamedProperty(PropertyHelper.NEW_USER_ROLES);
    if (!PropertyHelper.isEmpty(property) && !PropertyHelper.NONE_VALUE.equals(property.value)) {
        codes = TagHelper.convertToTagList(property.value, true);
        for (String code : codes) {
            if (roles == null) {
                roles = new ArrayList<Role>();
            }
            roles.add(new Role().code(code.toUpperCase()));
        }
        roles = RoleValidator.lookupAll(roles, PropertyHelper.NEW_USER_ROLES);
    }
    input.user.permissions = permissions;
    input.user.roles = roles;
    output.user = UserServiceProvider.provide().addUser(input.user);
    UserServiceProvider.provide().verifyAccount(output.user);
}
Also used : Role(com.willshex.blogwt.shared.api.datatype.Role) Permission(com.willshex.blogwt.shared.api.datatype.Permission) InputValidationException(com.willshex.gson.web.service.server.InputValidationException) Property(com.willshex.blogwt.shared.api.datatype.Property)

Example 7 with Role

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

the class UserService method toDocument.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.blogwt.server.service.search.IIndex#toDocument(java.lang.
	 * Object) */
@Override
public Document toDocument(User user) {
    Document document = null;
    if (user != null) {
        if (user.roleKeys != null) {
            user.roles = PersistenceHelper.batchLookupKeys(RoleServiceProvider.provide(), user.roleKeys);
        }
        if (user.permissionKeys != null) {
            user.permissions = PersistenceHelper.batchLookupKeys(PermissionServiceProvider.provide(), user.permissionKeys);
        }
        Document.Builder documentBuilder = Document.newBuilder();
        documentBuilder.setId(getName() + user.id.toString()).addField(Field.newBuilder().setName("username").setAtom(user.username)).addField(Field.newBuilder().setName("name").setText(UserHelper.name(user))).addField(Field.newBuilder().setName("forename").setText(user.forename)).addField(Field.newBuilder().setName("surname").setText(user.surname)).addField(Field.newBuilder().setName("email").setText(user.email)).addField(Field.newBuilder().setName("created").setDate(user.created)).addField(Field.newBuilder().setName("group").setText(user.group)).addField(Field.newBuilder().setName("summary").setText(user.summary));
        if (user.roles != null) {
            for (Role role : user.roles) {
                documentBuilder.addField(Field.newBuilder().setName("role").setText(role.name));
            }
        }
        if (user.permissions != null) {
            for (Permission permission : user.permissions) {
                documentBuilder.addField(Field.newBuilder().setName("permission").setText(permission.name));
            }
        }
        document = documentBuilder.build();
    }
    return document;
}
Also used : Role(com.willshex.blogwt.shared.api.datatype.Role) Permission(com.willshex.blogwt.shared.api.datatype.Permission) Document(com.google.appengine.api.search.Document) ScoredDocument(com.google.appengine.api.search.ScoredDocument)

Example 8 with Role

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

the class RoleService method addRole.

@Override
public Role addRole(Role role) {
    if (role.created == null) {
        role.created = new Date();
    }
    Key<Role> key = provide().save().entity(role).now();
    role.id = keyToId(key);
    return role;
}
Also used : Role(com.willshex.blogwt.shared.api.datatype.Role) Date(java.util.Date)

Example 9 with Role

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

the class GetRolesResponse method fromJson.

@Override
public void fromJson(JsonObject jsonObject) {
    super.fromJson(jsonObject);
    if (jsonObject.has("roles")) {
        JsonElement jsonRoles = jsonObject.get("roles");
        if (jsonRoles != null) {
            roles = new ArrayList<Role>();
            Role item = null;
            for (int i = 0; i < jsonRoles.getAsJsonArray().size(); i++) {
                if (jsonRoles.getAsJsonArray().get(i) != null) {
                    (item = new Role()).fromJson(jsonRoles.getAsJsonArray().get(i).getAsJsonObject());
                    roles.add(item);
                }
            }
        }
    }
    if (jsonObject.has("pager")) {
        JsonElement jsonPager = jsonObject.get("pager");
        if (jsonPager != null) {
            pager = new Pager();
            pager.fromJson(jsonPager.getAsJsonObject());
        }
    }
}
Also used : Role(com.willshex.blogwt.shared.api.datatype.Role) JsonElement(com.google.gson.JsonElement) Pager(com.willshex.blogwt.shared.api.Pager)

Example 10 with Role

use of com.willshex.blogwt.shared.api.datatype.Role 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")) {
        String group = REQUEST.get().getParameter("group");
        if (group == null || group.trim().isEmpty()) {
            group = "blogwt";
        }
        PageServiceProvider.provide();
        PostServiceProvider.provide();
        UserServiceProvider.provide();
        SearchHelper.indexAll((ISearch<?>) ServiceDiscovery.getService(group + "." + action.replace("index", "")));
    } else if ("clearsearch".equals(action)) {
        PersistenceServiceProvider.provide();
        String name = REQUEST.get().getParameter("index");
        String ids = REQUEST.get().getParameter("ids");
        String namespace = REQUEST.get().getParameter("ns");
        String[] split = ids.split(",");
        for (String id : split) {
            SearchHelper.deleteSearch(() -> namespace == null ? false : Boolean.valueOf(namespace).booleanValue(), 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;
        }
    } else if (action != null && action.startsWith("resave")) {
        String typeName = REQUEST.get().getParameter("type");
        ResaveServlet.queueForResaving(typeName);
    } else if (action != null && action.startsWith("deleteposts")) {
        processPaged((Integer s, Integer c, PostSortType o, SortDirectionType d) -> PostServiceProvider.provide().getPosts(Boolean.TRUE, Boolean.FALSE, s, c, o, d), PostServiceProvider.provide()::deletePost);
    }
}
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) JsonObject(com.google.gson.JsonObject) PostSortType(com.willshex.blogwt.shared.api.datatype.PostSortType) BlobKey(com.google.appengine.api.blobstore.BlobKey) ISearch(com.willshex.blogwt.server.service.search.ISearch) Permission(com.willshex.blogwt.shared.api.datatype.Permission) List(java.util.List) ArrayList(java.util.ArrayList) JsonParser(com.google.gson.JsonParser) Resource(com.willshex.blogwt.shared.api.datatype.Resource) MetaNotification(com.willshex.blogwt.shared.api.datatype.MetaNotification) SortDirectionType(com.willshex.blogwt.shared.api.SortDirectionType) Stack(com.willshex.blogwt.shared.page.Stack) Role(com.willshex.blogwt.shared.api.datatype.Role) GeneratedDownload(com.willshex.blogwt.shared.api.datatype.GeneratedDownload) Filter(com.willshex.blogwt.shared.page.search.Filter) Collection(java.util.Collection)

Aggregations

Role (com.willshex.blogwt.shared.api.datatype.Role)12 Permission (com.willshex.blogwt.shared.api.datatype.Permission)8 JsonElement (com.google.gson.JsonElement)4 User (com.willshex.blogwt.shared.api.datatype.User)4 Date (java.util.Date)3 InputValidationException (com.willshex.gson.web.service.server.InputValidationException)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 SafeHtml (com.google.gwt.safehtml.shared.SafeHtml)1 Column (com.google.gwt.user.cellview.client.Column)1 TextColumn (com.google.gwt.user.cellview.client.TextColumn)1 Key (com.googlecode.objectify.Key)1 GetPostsActionHandler (com.willshex.blogwt.server.api.blog.action.GetPostsActionHandler)1 ISearch (com.willshex.blogwt.server.service.search.ISearch)1 Pager (com.willshex.blogwt.shared.api.Pager)1 SortDirectionType (com.willshex.blogwt.shared.api.SortDirectionType)1 GetPostsRequest (com.willshex.blogwt.shared.api.blog.call.GetPostsRequest)1