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?
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.
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
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;
}
}
Looks good and no you don't need to do anymore than pass $searchCriteria to parent::getSearchQuery(); to get the $query object
You should also implement the canEdit method for your tutorial dataobject.
goo point dpde...
function canEdit () {return true;]
also canView
*maybe* canDelete
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);
}