douchen2025 2016-07-13 00:30
浏览 34

Silverstripe:用户特定的文件模块

I've got an idea for a project and wondering if there's an already existing module that can achieve something similar. Basically I want to allow the users to upload/download files from their member profile. Users will only be able to view/download their own files. I have member profiles set up already, it's the file handling that I need to get working now.

I imagine it will be something like a gridfield displayed on the users profile. When they use an upload field it will save files in to assets/ folder. Which can only be viewed by that member (admins can view all in the /admin section of the CMS.

Are there any current modules that support something like this?

If I create an extension of the Member profile that allows uploading files, how could I implement the security for only users with the currentID to view files?

EDIT: OK So I've made some progress with this. The upload file function works. It saves it in to a folder which is named after the user. However, it saves the uploaded file in that directory, but also a blank file under the "Files" directory. Here is my code...

//User file upload function
    public function UploadUserFile() {
        $fields = new FieldList($field = new UploadField('UserFiles', 'Upload files'));
        $field->setCanAttachExisting(false); // Block access to SilverStripe assets library
        $field->setCanPreviewFolder(false); // Don't show target filesystem folder on upload field
        $field->setFolderName('user-files/user-'.Member::currentUserID()); //Upload to a user specific folder
        $actions = new FieldList(new FormAction('submit', 'Save Images'));
        $form = new Form($this, 'UploadUserFile', $fields, $actions, null);
        return $form;
    }

    public function submit($data, $form) {
        $file = new File();
        $form->saveInto($file);
        $file->write();
        return $this;
    }

EDIT 2:

Turns out that the submit function was causing the extra file. Disregard. Still experiencing other problems now but will see if I can fix it first.

EDIT 3:

OK, I've had a go and I am able to display files on the front end. Here is my code...

//User file upload function
    public function UploadUserFile() {
        $fields = new FieldList($field = new UploadField('UserFiles', 'Upload files'));
        $field->setCanAttachExisting(false); // Block access to SilverStripe assets library
        $field->setCanPreviewFolder(false); // Don't show target filesystem folder on upload field
        $field->setFolderName('User-Files/User-'.Member::currentUserID().'-'.Member::currentUser()->Surname.Member::currentUser()->FirstName); //Upload to a user specific folder
        $field->setAutoUpload(false);
        $actions = new FieldList(new FormAction('submit', 'Save Images'));
        $form = new Form($this, 'UploadUserFile', $fields, $actions, null);
        return $form;
    }
    //Refresh files function
    public function submit($data, $form) {
        return $this->redirectBack();
    }
    //Display users files
    public function DisplayFiles() {
        $arrayList = ArrayList::create();
        $files = File::get()->filter(array(
            "OwnerID" => Member::currentUserID(),
            "ClassName:not" => 'Folder'
        ));
        foreach($files as $file) {

            if($file->canView()) {
                $arrayList->push($file);
            }
        }
        return $arrayList;
    }

I'm trying to allow users to delete their file from the front end. What's the best way to handle this?

Thanks

  • 写回答

1条回答 默认 最新

  • doufuhao8085 2016-07-13 00:43
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题