php - OctoberCMS 和 MySQL 的用户记录所有权

标签 php mysql laravel laravel-5 octobercms

我正在使用OctoberCMS基于Laravel ,跟官方User插件。

我正在制作一个画廊,前端用户可以在其中上传文件。

用户是其上传文件的所有者,并且是唯一有权编辑的人。

<小时/>

我的问题,这是设计和处理用户记录所有权的正确方法吗?所有用户上传记录将混合在一个数据库表 mysite_gallery_ 中,并通过过滤器排序并以 html 形式显示,例如查看特定用户名的所有上传。

这会很慢吗?每个用户应该有自己的表吗?它是否足够安全,可以防止其他用户、机器人或黑客脚本编辑不属于他们的文件记录?

<小时/>

MySQL 表

所有上传记录都保存到表mysite_gallery_中。

| id   | username   | filename   | slug     | title     | tags                 |
| ---- | ---------- | ---------- | -------- | --------- | -------------------- |
| 1    | matt       | xyz123     | xyz123   | My File   | space, galaxy, stars | 
<小时/>

记录所有权

上传时,我的自定义上传组件使用 Laravel 在数据库中创建文件的 titleslugtags 等记录.

为了定义所有权,我让上传组件将用户的用户名保存到记录中。

# Get Current User
$user = '';
if (Auth::check()) {
    $user = Auth::getUser();
    $user = $user->username;
}

# Create Record
$gallery = new Gallery();
$gallery->username = $user;
$gallery->filename = $name;
$gallery->title = $title;
$gallery->slug = $slug;
$gallery->tags = $tags;
$gallery->save();
<小时/>

编辑记录

如果用户想要编辑文件属性,例如标题,Laravel 会检查当前用户是否与记录中的用户名匹配。如果用户是所有者,则允许编辑。

# Get File Record Owner
$owner = '';
if (Gallery::where('filename', '=', $filename)->exists()) {

    $record = Gallery::where('filename', '=', $filename)->first();
    $owner = $record->username;

}

# Authenticate Current User is Owner
$is_owner = false;
if (Auth::check()) {

    # Get Current User
    $user = Auth::getUser();

    # Check if User is Owner
    if ($user->username == $owner) {
        $is_owner = true;
    }
}

# Edit Record
if ($is_owner == true) {

    # Update Record
    Gallery::where('filename', '=', $filename)->update(['title' => $title]);

    return Redirect::back();
}

最佳答案

最好使用用户 ID 而不是用户名。它将占用更少的数据库空间并且速度更快。

此外,我会将标签放在另一个表中。尽管这取决于您如何使用标签。例如,如果您将它们放在另一个表中,那么获取某个标签的所有上传会更容易。

关于php - OctoberCMS 和 MySQL 的用户记录所有权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42895561/

相关文章:

php - 我的服务中的渲染 View

php - 从图像 OnClick 事件执行 PHP 函数

Php , laravel, socket - 水平缩放技术

php - Laravel/Eloquent - 当日期过去时

php - Doctrine ORM ManyToOne Inverse 不起作用

php - 如何从 php 页面返回响应

laravel - "error": "invalid_grant" Laravel 7 Passport

sql - 在原始 sql 查询 Laravel 中插入变量

php - PHP7 中的可空返回类型

php - 根据时间将数据拆分为三个数组