2017-02-17 82 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

謝謝。它工作得很好。 –