简体   繁体   English

grails中基于角色的域类字段访问

[英]Role based domain class field access in grails

I am developing a grails application.In that some cases I want to control the domain class fields based on the role.So that in each call to getter setter method of domain class I want to apply some filter based on role(Logged in user's role).I am assuming that grails will create getter setter method at runtime for the domin classes.So while writing grails code is it possible to apply this logic.If it is possible then how to apply? 我正在开发一个grails应用程序。在某些情况下我想根据角色控制域类字段。所以在每次调用域类的getter setter方法时我想根据角色应用一些过滤器(登录用户的角色)我假设grails将在运行时为domin类创建getter setter方法。所以在编写grails代码时可以应用这个逻辑。如果有可能那么如何应用?

Example: 例:

Domain Class : 域类:

class Book{
   String name;
   double price;

  }

Controller: 控制器:

def index={
  Book book=Book.get(1);
   println book.name;
   println book.price;
 }

In the above code "println book.price;" 在上面的代码“println book.price;” this line should work only for particular role.For some other role it should throw some exception. 此行应仅适用于特定角色。对于其他角色,它应该抛出一些异常。

Is it possible achieve?Is there any plugin to do this? 有可能实现吗?有没有插件可以做到这一点?

Please give some help on this....Thanks 请给我一些帮助....谢谢

You can create get/set methods for the properties you want to control access to and put your security logic there. 您可以为要控制访问的属性创建get / set方法,并将安全逻辑放在那里。 Assuming you've written your own security service or are using a security plugin like the Spring Security (Acegi) plugin you would: 假设您已编写自己的安全服务或正在使用Spring Security(Acegi)插件等安全插件,您将:

class Book{
    String name;
    double price;

    def authenticateService

    void setPrice(double price) {
        if(!authenticateService.ifAllGranted('ROLE_PRICE_FIXER')) {
            throw new Exception("You are not authorized to set book prices")
        }
        this.price = price
    }

    double getPrice() {
        if(!authenticateService.ifAllGranted('ROLE_PRICE_FIXER')) {
            throw new Exception("You are not authorized to get book prices")
        }
        return this.price
    }
}

I am not aware of any plugin that allows access controls to be put on domain properties. 我不知道任何允许访问控件放在域属性上的插件。

You could also consider using a custom validator or a spring errors object to catch attempts to set a field before saving it. 您还可以考虑使用自定义验证程序或弹簧错误对象来捕获在保存之前设置字段的尝试。

EDIT: Here is an example of what I was thinking. 编辑:这是我在想的一个例子。 You could generalize quite a bit more and the code here hasn't been tested so it probably won't run as is. 你可以概括一点,这里的代码还没有经过测试,所以它可能不会按原样运行。

class securedDomain {
    String securedField

    def fieldSetBy = [:]
    def previousValue = [:]
    static transients = ['fieldSetBy', 'previousValue']

    static constraints = {
        securedField(validator: { v, o ->
             def access = User.findByName(fieldSetBy['securedField']).hasAccess('securedField')
             if(!access) securedField = previousValue['securedField']
             return access
        })

    void setProperty(String name, value) {
        if(name == "securedField") {
            fieldSetBy['securedField'] = session.user
            previousValue['securedField'] = securedField
            securedField = value
        } else {
            super(name, value)
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM