Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Assign a user to a dataobject record

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

Page: 1
Go to End
Author Topic: 409 Views
  • sajok
    Avatar
    Community Member
    82 Posts

    Assign a user to a dataobject record Link to this post

    Hello,

    I have AdminModel section to create and manage schools. Now I need to assign a user to a specific school record, so they can only see that recod and edit it. Administrators can create schools and assign a user to them, but the assigned user can only see their school.

    I'm thinking of adding a dropdown field of site users in the school creation page, but I need help on how I can assign permission to them.

    Any idea how to accomplish this?

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Assign a user to a dataobject record Link to this post

    So your users are already in the CMS? First step is to break your project into steps and work on each step. Assigning users to a school is done via relationships (http://doc.silverstripe.org/framework/en/tutorials/5-dataobject-relationship-management) and permission control is done via can*() methods. For example:

    <?php

    class School extends DataObject {

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

    function canEdit($member = null) {
    if(!$member) $member = Member::currentUser();

    if($member->ID == $this->ManagerID) {
    return true;
    }

    return Permission::check('ADMIN');
    }

    function canDelete($member = null) {
    return $this->canEdit($member);
    }

    }

  • sajok
    Avatar
    Community Member
    82 Posts

    Re: Assign a user to a dataobject record Link to this post

    Thanks Willr, that helps a lot..

    I created a dropdown field in the school dataobject so I can assign a user to manage the school, but when I select a user from the dropdown field it doesn't seem to be selected after clicking save:

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

    public function getCMSFields() {
    return new FieldList(
    <...>
    new DropdownField('MemberID', 'School Admin', DataList::create("Member")->map("ID", "Title"))
    );
    }

    I have done this other object that I created with no problem, but in the case of Member class, do I need to sub-class it just to add a has_one relationship with the School DataObject?

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Assign a user to a dataobject record Link to this post

    That looks fine, have you run a /dev/build to rebuild your database?

  • sajok
    Avatar
    Community Member
    82 Posts

    Re: Assign a user to a dataobject record Link to this post

    After running dev/build the user now is assigned to School dataobject.

    Now when a manager edits their school record they see the dropdown field where the user is assigned to the school. I want this field to be hidden from school managers or to be shown as readonly field.

    Any idea how to make this possible?

    409 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.