Zend ACL - 如何通过 ajax/jquery 处理对受保护页面的请求
我有点被困在这里 - 我可以写一个黑客来解决这个问题,但想知道是否有适当的方法来做到这一点。
我使用 Zend ACL 来指定哪些页面受到保护并需要身份验证。 例如,如果“UploadVideo.php”是受保护的页面,则当用户单击链接访问该页面时,他将被带到登录页面,登录后将重定向回 UploadVideo.php 页面。所以这一切都得到了照顾并且工作正常。
现在在另一页上 - 我有一个类似“投票”按钮的东西。用户只有登录后才能投票。
案例 1 当用户登录时 - 他单击“投票”按钮 - 我使用 Jquery 按以下方式调用操作: <代码> $.post('/video/vote', {video_id:video_id}, 函数(数据) { 如果(数据==“确定”)...... } 现在 /video/vote 是受保护的资源,并且由于用户已登录 - 投票操作被调用,这会增加投票计数并发回“OK”消息,并且页面会使用新的投票计数动态更新。
案例2 用户未登录 - 他单击投票按钮 - 由于页面受到保护 - 登录页面通过变量“data”中的 ajax 调用返回 理想情况下,当用户未登录时 - ajax 调用不应该发生 - 用户应该被重定向到登录页面 - 登录后,他应该被重定向到 /video/vote 操作,这将增加投票计数 - 最后用投票按钮发送回页面
我该如何处理。我可以编写一些 hack 并检查用户是否登录,并根据情况决定是否进行 ajax 调用或重定向用户,但这是执行此操作的最佳方法。
不知道我有没有把问题解释清楚。
感谢您抽出时间
I'm kind of stuck here - I can write a hack to work around this but was wondering if there is a proper way to do this.
I am using Zend ACL to specify which pages are protected and require authentication.
For example if the "UploadVideo.php" is a protected page - when a user clicks on a link to access that page - he is taken to the login page and after login is redirected back to UploadVideo.php page. So this all is taken care of and works fine.
Now on this other page - I have something like a "VOTE" button. A user can only cast vote if he is logged in.
Case 1
When the user is logged in - he clicks on the Vote button - I am using Jquery to call the action the following way:
$.post('/video/vote', {video_id:video_id}, function(data) {
if(data=="OK") .....
}
Now /video/vote is a protected resource and since the user is logged in - the vote action gets called which increases the vote count and sends back an "OK" message and the page is dynamically updated with the new vote count.
Case 2
The user is not logged in - he clicks on the vote button - since the page is protected - the login page is returned thru the ajax call in variable "data"
Ideally, when the user is not logged in - the ajax call should not happen
- the user should be redirected to the login page
- after login he shud be redirected to the /video/vote action which will increment the vote count
- and finally sent back to the page with the vote button
How do I handle this. I can write some hacks and check if user is logged in or not and depending upon that decide whether to make ajax call or redirect user but is that the best way to do this.
I dont know if I have clearly explained the problem.
Thanks for your time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道可能有一种 Zend 特定的方法,但我想不出比
Populate a Javascript variable
logged_in
更简单(更干净)的方法,具体取决于用户是否是否登录当用户单击时检查
logged_in
投票按钮;如果没有,则提供重定向使用包含当前页面 URL 的目标变量重定向到登录页面(对于,我确信存在 Zend Framework 特定的方式)
显然,保留所有服务器端检查地方。这只是为了方便。
There may be a Zend specific way for this I don't know about, but I can't think of anything simpler (and cleaner) than
Populate a Javascript variable
logged_in
depending on whether the user is logged in or notDo a check for
logged_in
when the user clicks the vote button; offer to redirect if they aren'tRedirect to the login page with a target variable containing the current page's URL (for that, I'm sure, a Zend Framework specific way exists)
obviously, keep all server side checks in place. This is just for convenience.