【问题标题】:Allowing only the current user to the edit page in grails只允许当前用户进入 grails 中的编辑页面
【发布时间】:2014-03-12 23:10:05
【问题描述】:

我正在使用 spring 安全性在 grails 中构建一个 Web 门户。在我的门户中 具有USERADMIN 角色的用户有权编辑他们的个人资料。通过以 USER 角色登录,进入编辑页面/RITE/user/myedit/2 并可以进行编辑。问题是当我通过/RITE/user/myedit/1 编辑网址/RITE/user/myedit/2 时,我能够看到ADMI 特权用户。我尝试了注释,但没有用。我怎样才能避免这种情况?

@Secured(['ROLE_ADMIN','ROLE_USER'])
def mylist(Integer max)
{
    def user = springSecurityService.currentUser
    def c= User.findByUsername(user.username)
    params.max = Math.min(max ?: 10, 100)
    [userInstanceList: c]

}
@Secured(['ROLE_ADMIN','ROLE_USER'])
def myshow(Long id) {
    //userService.show(id)
    //println "in myshow"
    def user = springSecurityService.currentUser
    UserDetails currentUser = springSecurityService.principal
            if(id==currentUser.id){
            def userInstance = User.get(id)
            if (!userInstance) {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
                redirect(action: "mylist")
                return
            }
        [userInstance: userInstance]
        }
    }
@Secured(['ROLE_ADMIN','ROLE_USER'])
def myedit(Long id) {
    def user = springSecurityService.currentUser
    UserDetails currentUser = springSecurityService.principal
    def c= User.findByUsername(user.username)
        if(id == user.id ){
        def userInstance = User.get(c.id)
        if (!userInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
            println("not allowed")
            redirect(action: "mylist")
            return
        }

        [userInstance: userInstance]
    }
}

【问题讨论】:

  • 你写“我试过注释,但没用”——你试过什么? - 你应该发布你试过的!
  • @Ralph 我编辑了我的帖子

标签: spring url grails spring-security


【解决方案1】:

您需要手动实现以下内容:

if( User.findByUsername(springSecurityService.currentUser.username).id
          != id_ofEditedUser) {
    throw new AccessDeniedException("one can only edit its own stuff");
}

【讨论】:

    【解决方案2】:

    Ralph 的建议是准确的,但当我们谈论只能由经过身份验证的用户编辑的用户信息时,我通常不会这样做。你最好不要身份证:

    def myedit() {
    
       // this is the authenticated user, therefor, this is the information being edited
       def userInstance = User.findByUsername(springSecurityService.principal.username)
       [userInstance: userInstance]
    
    }
    

    如果您希望特定用户以外的其他人能够编辑用户信息,例如管理员,请为此提供专门的管理操作。从现在起 6 个月后重新查看代码会更有意义。

    【讨论】:

    • 我为管理员提供了编辑其他人的权限。真的很有帮助。
    猜你喜欢
    • 2015-01-01
    • 2021-06-19
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多