Jump to:

5449 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » How do I set admins to only viewing/editing their own records

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 321 Views
  • Arbee
    Avatar
    Community Member
    17 Posts

    How do I set admins to only viewing/editing their own records Link to this post

    I'm working in 2.4.5 in ModelAdmin. I have a number of tutors writing their own tutorials. When they enter the admin section I want them only to see their own tutorial records and not those of other tutors. How do I do that?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: How do I set admins to only viewing/editing their own records Link to this post

    Disclaimer - this isn't *the* way to do it but *a* way - I'm looking to tidy in the move to SS3... With that said...

    class MyAdmin extends ModelAdmin {
       static $managed_models = array('MyObject');
       static $collection_controller_class = "MyAdmin_CollectionController";
    ...
    }

    class MyAdmin_CollectionController extends ModelAdmin_CollectionController {

       function getSearchQuery($searchCriteria) {
          $query = parent::getSearchQuery($searchCriteria);

          if ($this->modelClass == 'MyObject'){
             $query->where[] = 'MyObject.MemberID='.Member::currentUserID();
          }

          return $query;
       }
    }

    Hope this helps

  • Arbee
    Avatar
    Community Member
    17 Posts

    Re: How do I set admins to only viewing/editing their own records Link to this post

    Thanks so much for your help Swaiba!

    So my code should look something like this. Am I missing anything? do I need anything for $searchCriteria?

    class TutorEditAdmin extends ModelAdmin {
       
          static $managed_models = array('TutorEdit');
          static $collection_controller_class = "TutorEditAdmin_CollectionController";
          
          static $url_segment = 'tutorEdit';
          
          
       
       }

       class TutorEditAdmin_CollectionController extends ModelAdmin_CollectionController {

          function getSearchQuery($searchCriteria) {
             $query = parent::getSearchQuery($searchCriteria);

             if ($this->modelClass == 'TutorEdit'){
                $query->where[] = 'TutorEdit.MemberID='.Member::currentUserID();
             }

             return $query;
          }
       }

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: How do I set admins to only viewing/editing their own records Link to this post

    Looks good and no you don't need to do anymore than pass $searchCriteria to parent::getSearchQuery(); to get the $query object

  • dpde
    Avatar
    Community Member
    14 Posts

    Re: How do I set admins to only viewing/editing their own records Link to this post

    You should also implement the canEdit method for your tutorial dataobject.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: How do I set admins to only viewing/editing their own records Link to this post

    goo point dpde...

    function canEdit () {return true;]
    also canView

    *maybe* canDelete

  • dpde
    Avatar
    Community Member
    14 Posts

    Re: How do I set admins to only viewing/editing their own records Link to this post

    Of course canDelete too.
    Something like this should work (not tested):

    public function canEdit($member) {
    return (Permission::check('ADMIN') || $this->MemberID == $member->ID);
    }

    public function canDelete($member) {
    return (Permission::check('ADMIN') || $this->MemberID == $member->ID);
    }

    321 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.