dongming6201 2015-09-09 07:20
浏览 41
已采纳

如何保护未登录Yii2项目的任何人下载文件?

Basically, i'm using a standard Yii2 set up. i have a login that allows a user to see links to files. the files are in a download folder.. but the page to see the links is only able to be seen if a user is "not guest." now, if anyone just copies the link to someone else, that other person will be able to download the file without being logged in. the files in question aren't being "paid for" BUT i am required to have people login before they are allowed to download them becuase of terms of service and other laws. can i dynamically create download links via php? at least check for "isGuest" and if isGuest = True then redirect to login page. how would i do that instead of just having a direct download link?

  • 写回答

3条回答 默认 最新

  • dongzhenjian5195 2015-09-09 08:20
    关注

    You can provide acces to an specific action to specific roles or users with AccessControl Class .

    You can do something like this:

    class DownloadController extends Controller
    {
        public function behaviors()
        {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'allow' => false,
                            'actions' => ['download'],
                            'roles' => ['?'],
                        ],
                    ],
                ],
            ];
        }
        // ...
    }
    

    '?' Role are the guest users.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图