2017-02-17 83 views
0

我有喜欢的网址 http://192.168.0.226:8080/crm/contacts/contactProfile/5如何阻止访问特定的网址Grails中

公司可以创建一个帐户,然后创建联系人。它会生成URL中使用的联系人ID。而任何一家公司可以只通过更换ID的随机数一样

http://192.168.0.226:8080/crm/contacts/contactProfile/675

如何防止一个公司到另一个公司中的联系人访问另一家公司的联系人。而且,如果ID不在数据库中,它将显示错误。如果id不在那里,我如何重定向到404页面。

我正在使用grails 2.2.1以及spring security。我试图通过requestmap解决它

def structureMap2 = Requestmap.findByUrl("contacts/contactProfile/*") ?: new Requestmap(url: "contacts/contactProfile/*",configAttribute: "ROLE_COMPANY").save(failOnError:true) 

但它没有奏效。如果我不得不重构URL,我该如何做。或者还有另一种方式来解决这个问题。谢谢。

回答

0

那么,我会在Contacts/contactProfile控制器方法。 检查访问它的用户是否有权打开它。如果是,你渲染页面,如果没有,还给了flash.error(或任何其他)和重定向到404。事情是这样的:

def contactProfile() { 
    def user = = springSecurityService.currentUser 
    def contact = Contact.get(params.id) 
    if (!contact) { 
     flash.error = "There is no such contact!" 
     redirect(controller: "errors", action: "404") 
    } else if (contact.company.id == user.company.id) { 
     //create the stuff 
     [contact: contact] //render the page 
    } else { 
     flash.error = "You are not authorised to view this contact!" 
     redirect(controller: "errors", action: "404") 
    } 
} 

这是写假设你有公司分配给用户和联系人也有它创建的公司。

+0

谢谢。它工作得很好。 –