Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 仅允许当前用户在grails中编辑页面_Spring_Url_Grails_Spring Security - Fatal编程技术网

Spring 仅允许当前用户在grails中编辑页面

Spring 仅允许当前用户在grails中编辑页面,spring,url,grails,spring-security,Spring,Url,Grails,Spring Security,我正在用grails和spring security构建一个web门户。在我的门户中,具有用户,管理员角色的用户有权编辑其个人资料。以用户角色登录,进入编辑页面/RITE/USER/myedit/2,即可进行编辑。问题是当我编辑url/RITE/user/myedit/2时,通过/RITE/user/myedit/1我能够看到行政管理特权用户。我试过注释,但没用。我怎样才能避免这种情况 @Secured(['ROLE_ADMIN','ROLE_USER']) def mylist(Integer

我正在用grails和spring security构建一个web门户。在我的门户中,具有
用户
管理员
角色的用户有权编辑其个人资料。以用户角色登录,进入编辑页面
/RITE/USER/myedit/2
,即可进行编辑。问题是当我编辑url
/RITE/user/myedit/2
时,通过
/RITE/user/myedit/1
我能够看到行政管理特权用户。我试过注释,但没用。我怎样才能避免这种情况

@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]
    }
}

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

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

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个月后,当您必须再次查看代码时,这将更有意义。

您编写了“我尝试了注释,但没有成功”--您尝试了什么你应该发布你尝试过的信息@拉尔夫:我编辑了我的帖子。我为管理员提供了编辑其他帖子的特权。真的很有帮助。