Skip to content

Commit

Permalink
Merge pull request #13 from agorapulse/Micronaut-permissions-Result-r…
Browse files Browse the repository at this point in the history
…equires-permission-inheritance

Add inherited annotation on ResultRequiresPermission annotation so cl…
  • Loading branch information
Rigerwal authored Jul 27, 2023
2 parents e294a73 + 4d0c310 commit f5811c2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

Expand All @@ -33,6 +34,7 @@
* and every check must pass.
*/
@Around
@Inherited
@Documented
@Retention(RUNTIME)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2023 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.permissions;

import java.util.Collection;
import java.util.Map;

public interface IPostService {

Post create(Long userId, String message);

@ResultRequiresPermission(value = "view") // <2>
Post get(Long id);

@RequiresPermission("edit")
Post archive(Post post);

@RequiresPermission("edit")
void handleIterableContainer(Collection<Post> posts);

@RequiresPermission("edit")
void handleContainerNonIterable(Post post, Map<String, String> couldBeIterableContainer);

@RequiresPermission("edit")
Post publish(Post post);

@ResultRequiresPermission(value = "view", returnNull = true) // <3>
Post getOrEmpty(Long id);

@RequiresPermission("read")
Post merge(Long userId, Post post1, Post post2);

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@Controller("/post")
public class PostController {

private final PostService postService;
private final IPostService postService;
private final PostRepository postRepository;

public PostController(PostService postService, PostRepository postRepository) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,51 @@
import java.util.Map;

@Singleton
public class PostService {
public class PostService implements IPostService {

private final PostRepository postRepository;

public PostService(PostRepository postRepository) {
this.postRepository = postRepository;
}

@Override
public Post create(Long userId, String message) {
if (userId == null || userId == 0) {
throw new IllegalArgumentException("User not specified");
}
return Post.createDraft(userId, message);
}

@RequiresPermission("edit") // <1>
@Override // <1>
public Post archive(Post post) {
return post.archive();
}

@RequiresPermission("edit")
@Override
public void handleIterableContainer(Collection<Post> posts) {
}

@RequiresPermission("edit")
@Override
public void handleContainerNonIterable(Post post, Map<String, String> couldBeIterableContainer) {
}


@RequiresPermission("edit")
@Override
public Post publish(Post post) {
return post.publish();
}

@ResultRequiresPermission(value = "view") // <2>
@Override
public Post get(Long id) {
return postRepository.get(id);
}

@ResultRequiresPermission(value = "view", returnNull = true) // <3>
@Override
public Post getOrEmpty(Long id) {
return postRepository.get(id);
}

@RequiresPermission("read")
@Override
public Post merge(Long userId, Post post1, Post post2) {
return Post.createDraft(userId, post1.getMessage() + post2.getMessage());
}
Expand Down

0 comments on commit f5811c2

Please sign in to comment.