{"id":3477,"date":"2011-04-06T16:16:05","date_gmt":"2011-04-06T10:46:05","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3477"},"modified":"2016-12-16T13:01:19","modified_gmt":"2016-12-16T07:31:19","slug":"handling-instance-based-security","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/handling-instance-based-security\/","title":{"rendered":"Handling Instance Based Security"},"content":{"rendered":"<p>In my current project, we were required to implement <strong>Instance Based Security<\/strong>. The idea was to find a clean solution separate from the main business logic of the application. We took a clue from the <strong>Spring Security Plugin<\/strong> to use the <strong>Annotations<\/strong> to do our job. All we wanted to do was to develop annotations for actions, which could help to decide what type of access verification needs to be done on a particular request, inside some Filter. So we proceeded like this:<\/p>\n<p>We created an Abstract Class from which our <strong>User<\/strong> domain Class extended. All our Access Verifying functions would remain in this class. The class provides a single entry function (by the name <strong>evaluateExpression<\/strong>) to all other Access Verifying functions. This helped us to keep the Filter class clean. The Class looks like:<\/p>\n<p>[java]<br \/>\nabstract class MyAppSecure {<\/p>\n<p>  public Boolean evaluateExpression(String methodName, Map params = [:]) {<br \/>\n    this.&quot;$methodName&quot;(params.id?.toLong())<br \/>\n  }<\/p>\n<p>  \/\/ One of the many functions to be used to verify valid access<br \/>\n  public Boolean hasUserAccessById(Long id) {<br \/>\n    return id ? (this.id == id.toLong()) : true<br \/>\n  }<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>So our User domain class looks like:<\/p>\n<p>[java]<br \/>\nclass User extends MyAppSecure {<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>Now, the idea is to:<\/p>\n<ol>\n<li>Intercept a request in the filter.<\/li>\n<li>Get the annotation expression placed on top of the respective Controller Action. (The expression(s) would be name of the <strong>MyAppSecure<\/strong> method(s) used to verify the access)<\/li>\n<li>Call a corresponding method in <strong>MyAppSecure<\/strong> to verify access<\/li>\n<li>Take appropriate action corresponding to a result<\/li>\n<\/ol>\n<p>So let us delve into the implementation:<\/p>\n<p>First step would be to enable the annotations. To do this create an interface in src\/java (or you can do it in src\/groovy as well):<\/p>\n<p>[java]<br \/>\n@Documented<br \/>\n@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})<br \/>\n@Retention(RetentionPolicy.RUNTIME)<br \/>\npublic @interface MySecured {<br \/>\n    String[] expressions();<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Now we are good to go for using annotations. Let us assume an action <strong>edit<\/strong> inside <strong>AccountController<\/strong>:<\/p>\n<p>[java]<br \/>\nclass AccountController{<\/p>\n<p>@MySecured(expressions = [&quot;hasUserAccessToAccount&quot;])<br \/>\n def edit = {<br \/>\n    User user = params.id ? User.get(params.id) : user<br \/>\n    render(view: &quot;edit&quot;, model: [user: user])<br \/>\n  }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p><strong>Note<\/strong>: We can also give multiple expressions (to evaluate multiple access rules), separated by\u00a0commas inside the list.<\/p>\n<p>Now we need to implement a method by the name <strong>hasUserAccessToAccount<\/strong> in MyAppSecure:<\/p>\n<p>[java]<br \/>\nabstract class MyAppSecure {<\/p>\n<p>public Boolean evaluateExpression(String methodName, Map params = [:]) {<br \/>\n    this.&quot;$methodName&quot;(params)<br \/>\n}<\/p>\n<p>public Boolean hasUserAccessToAccount(Map params) {<br \/>\n\/\/ SAMPLE LOGIC.<br \/>\n    Integer count = Account.createCriteria().get {<br \/>\n      projections {<br \/>\n        count(&quot;id&quot;)<br \/>\n      }<br \/>\n      eq(&#8216;id&#8217;, params.id.toLong())<br \/>\n      eq(&#8216;user&#8217;, this)<br \/>\n    }<br \/>\n    return (count &gt; 0)<br \/>\n  }<br \/>\n }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>We would also need some logic to get and parse annotations in <strong>MyAppSecure<\/strong>. We wrote something like this:<\/p>\n<p>[java]<br \/>\npublic Boolean hasAccess(Class controllerClazz, String actionName,  Map params) { def field = controllerClazz.declaredFields.find {it.toString().indexOf(controllerClazz.name + &#8216;.&#8217; + actionName) != -1}<br \/>\n \/\/ get the annotation on a Controller action (account\/edit in our case)<br \/>\n    def securedAnnotation = field.getAnnotation(AdlSecured)<br \/>\n    return (securedAnnotation ? this.evaluateEveryExpression (params, securedAnnotation)<br \/>\n  }<\/p>\n<p> \/\/ Evaluate every expression and combine the result using AND (or we can use OR as well)<br \/>\n  private Boolean evaluateEveryExpression(Map params, def securedAnnotation) {<br \/>\n    Boolean hasAccess = securedAnnotation.expressions().every {String securedExpression -&gt;<br \/>\n      this.hasAccessForExpression(securedExpression, params)<br \/>\n    }<br \/>\n    return hasAccess<br \/>\n  }<\/p>\n<p>  \/\/Evaluate expression<br \/>\n  private Boolean hasAccessForExpression(String expressionToBeEvaluated, Map params) {<br \/>\n    return this.evaluateExpression(expressionToBeEvaluated, params)<br \/>\n  }<\/p>\n<p>[\/java]<\/p>\n<p>Now in you <strong>Filter<\/strong> you can write something like this:<\/p>\n<p>[java]<br \/>\nmySecureFilter(controller: &quot;*&quot;, action: &quot;*&quot;) {<br \/>\n      before = {<br \/>\n          User user = someSecurityService.currentUser<br \/>\n          if (user) {<br \/>\n          \/\/ gets the controller class for a controllerName<br \/>\n            Class controllerClazz = getControllerClass(controllerName)<\/p>\n<p>            if (!user.hasAccess(controllerClazz, actionName, params)) {<br \/>\n                redirect(controller: &#8216;accessController&#8217;, action: &#8216;unauthorized&#8217;)<br \/>\n                }<br \/>\n              }<br \/>\n              return false<br \/>\n            }<br \/>\n          }<br \/>\n[\/java]<\/p>\n<p>That is all that we need to do. I hope you would find this useful. Any suggestions will be welcomed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my current project, we were required to implement Instance Based Security. The idea was to find a clean solution separate from the main business logic of the application. We took a clue from the Spring Security Plugin to use the Annotations to do our job. All we wanted to do was to develop annotations [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[7],"tags":[521,4840,227,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3477"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3477"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3477\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}