Waffle Recipes

How-To's and Tutorials for the ActiveWAFL Framework

How to Allow or Restrict the Reading and/or Writing of Data Models

Posted Nov 20, 2014

By default, all data models are readable and writable by all users.
You can change this behavior by overriding the DoesCurrentUserHaveWriteAccess and DoesCurrentUserHaveReadAccess methods of the corresponding Functional Model.

Allow a Model to be Saved by Providing Write Access

Example Functional Model that allows anyone to read/write any instance
  1. namespace MyApp\FunctionalModel;
  2.  
  3. class MyModelClass
  4. extends \MyApp\DataModel\MyModelClass
  5. {
  6.     public function DoesCurrentUserHaveWriteAccess()
  7.     {
  8.         return true; //give global write access
  9.     }
  10. }
Example Functional Model that allows only users of a certain group to read/write any instance
  1. namespace MyApp\FunctionalModel;
  2.  
  3. class MyModelClass
  4. extends \MyApp\DataModel\MyModelClass
  5. {
  6.     public function DoesCurrentUserHaveWriteAccess()
  7.     {
  8.         return \Wafl\Core::$CURRENT_USER->Get_UserGroupId() == 3; //give global write access to anyone in user group 3
  9.     }
  10. }
Example Functional Model that allows only specific users to read/write only specific instances that they're related to
  1. namespace MyApp\FunctionalModel;
  2.  
  3. class MyModelClass
  4. extends \MyApp\DataModel\MyModelClass
  5. {
  6.     public function DoesCurrentUserHaveWriteAccess()
  7.     {
  8.         return \Wafl\Core::$CURRENT_USER->Get_UserId() == $this->_user->Get_UserId(); //give write access to users who own this particular instance
  9.     }
  10. }