alex
alex

Reputation: 307

How to customize CMS?

I have created simple dataobject:

class Documents extends DataObject {
     private static $db = array(
       'DocType' => 'Text',
       'ApprovalDate' => 'Date',
       'PublicationDate' => 'Date',
       'DocNumber' => 'Text',
       'DocTitle' => 'Text'

);

private static $has_one = array(
      'Member' => 'Member'
);

Give me please any idea how to customize my ModelAdmin so, that all users could only view all objects, and only owner (user with ID == MemberID) could edit and delete his objects? As the result I want to see such picture:[https://yadi.sk/i/o5Nys_szqnPtQ ]

I try to use such code:

if (!(Member::currentUserID() ==  $Value_of_MemberID_Field   )) {
    $gridfieldConfig->removeComponentsByType('GridFieldDeleteAction')
        ->removeComponentsByType('GridFieldEditButton');
    // add a view button
    $gridfieldConfig
            ->addComponent(new GridFieldViewButton());
}

How can I get $Value_of_MemberID_Field in the row of GridField?

Upvotes: 1

Views: 61

Answers (1)

kinglozzer
kinglozzer

Reputation: 606

You should probably look into model permissions: https://docs.silverstripe.org/en/3.3/developer_guides/model/permissions/.

For your example, it might look something like this:

public function canEdit($member = null) {
    return (Member::currentUserID() == $this->MemberID);
}

public function canDelete($member = null) {
    return (Member::currentUserID() == $this->MemberID);
}

Upvotes: 3

Related Questions