Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Assign a user to a dataobject record


Go to End


5 Posts   1383 Views

Avatar
sajok

Community Member, 82 Posts

3 May 2013 at 5:45am

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

Avatar
Willr

Forum Moderator, 5523 Posts

3 May 2013 at 8:38pm

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);
}

}

Avatar
sajok

Community Member, 82 Posts

4 May 2013 at 5:51am

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?

Avatar
Willr

Forum Moderator, 5523 Posts

4 May 2013 at 9:46am

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

Avatar
sajok

Community Member, 82 Posts

6 May 2013 at 2:41pm

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?