{"id":20963870,"url":"https://github.com/codemix/accessrestrictable","last_synced_at":"2025-08-08T04:10:08.923Z","repository":{"id":9826133,"uuid":"11812408","full_name":"codemix/AccessRestrictable","owner":"codemix","description":"A Yii ActiveRecordBehavior that automatically applies conditions for access restriction to every query.","archived":false,"fork":false,"pushed_at":"2013-10-10T16:08:29.000Z","size":240,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-24T01:45:00.078Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codemix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-08-01T08:59:05.000Z","updated_at":"2020-11-14T19:20:08.000Z","dependencies_parsed_at":"2022-09-22T14:41:12.513Z","dependency_job_id":null,"html_url":"https://github.com/codemix/AccessRestrictable","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/codemix/AccessRestrictable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2FAccessRestrictable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2FAccessRestrictable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2FAccessRestrictable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2FAccessRestrictable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codemix","download_url":"https://codeload.github.com/codemix/AccessRestrictable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2FAccessRestrictable/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269361337,"owners_count":24404358,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-08T02:00:09.200Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T02:48:41.839Z","updated_at":"2025-08-08T04:10:08.899Z","avatar_url":"https://github.com/codemix.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"AccessRestrictable\n==================\n\nThis behavior adds automatic access restriction to your ActiveRecord queries.\n\nBy doing so it introduces a new security layer right inside the models. If any user\ntries to access resources that he doesn't have permission to, the query result will simply be empty.\n\n## Installation\n\nWe recommend to install the extension with [composer](http://getcomposer.org/). Add this to\nthe `require` section of your `composer.json`:\n\n    'codemix/accessrestrictable' : 'dev-master'\n\n\u003e Note: There's no stable version yet.\n\nYou also need to include composer's autoloader on top of your `index.php`:\n\n    require_once __DIR__.'/protected/vendor/autoload.php';\n\nMake sure to fix the path to your composer's `vendor` directory. Finally you also need to\nconfigure an `alias` in your `main.php`:\n\n```php\n$vendor = realpath(__DIR__.'/../vendor');\nreturn array(\n    'alias' =\u003e array(\n        'AccessRestrictable' =\u003e $vendor.'/codemix/AccessRestrictable/src/AccessRestrictable',\n    ),\n    ...\n```\n\n## Configuration\n\nTo enable access restriction you have to attach the behavior to an ActiveRecord like so:\n\n```php\n\u003c?php\nclass Post extends CActiveRecord\n{\n    public function behaviors()\n    {\n        return array(\n            'restrictable' =\u003e array(\n                'class'             =\u003e 'AccessRestrictable\\Behavior',\n\n                // Optional settings with default values\n                // 'enableRestriction' =\u003e true\n            ),\n        );\n    }\n```\n\n### Restrict read access\n\nTo restrict *read access* for all your queries, you can implement a `beforeRead()` method\nin your record. It can return a `boolean` to either apply no restriction at all (`true`)\nor restrict access completely (`false`).\n\nThough the more common use case will be, to return a criteria that will be applied to\nall queries, and that limits the result set to records that the current user has access to.\nTo do so the method can either return a `CDbCriteria` or an array with criteria parameters.\n\nHere's an example:\n\n```php\npublic function beforeRead()\n{\n    $user   = Yii::app()-\u003euser;\n    $table  = $this-\u003egetTableAlias();\n\n    if($user-\u003echeckAccess('admin')) {\n        return true;    // no restriction for administrators\n    } elseif($user-\u003echeckAccess('organizationAdmin')) {\n        $userRecord = User::model()-\u003efindByPk(Yii::app()-\u003euser-\u003eid);\n\n        // Admins in an organisation are allowed to see all users from that organisation\n        return array(\n            'condition' =\u003e \"$table.organisation_id = :organisation\",\n            'params'    =\u003e array(':organisation' =\u003e $userRecord-\u003eorganisation_id),\n        );\n    } else {\n        // All other users can only query their own user record\n        return array(\n            'condition' =\u003e \"$table.user_id = :id\",\n            'params'    =\u003e array(':id' =\u003e Yii::app()-\u003euser-\u003eid),\n        );\n    }\n}\n```\n\n### Restrict write access\n\nIn order to restrict *write access* for records, you can implement a `beforeWrite()` method\nin your record. It will be called before any insert, update or delete operation and must\nreturns, whether the operation should be performed.\n\nFor example:\n\n```php\npublic function beforeWrite()\n{\n    $user   = Yii::app()-\u003euser;\n\n    if($user-\u003echeckAccess('admin')) {\n        return true;    // admin can always write\n    } elseif($user-\u003echeckAccess('organizationAdmin')) {\n        $userRecord = User::model()-\u003efindByPk(Yii::app()-\u003euser-\u003eid);\n\n        // Admins in an organisation are allowed to update all users from that organisation\n        if($userRecord-\u003eorganisation_id == $this-\u003eorganisation_id) {\n            return true;\n        }\n    } elseif($user-\u003eid==$this-\u003eid) {\n        // All users can update their own user record\n        return true;\n    }\n\n    // All others are denied\n    return false;\n}\n```\n\n## Usage\n\n\nIf you've attached the behavior, then whenever you do a query like\n\n\n```php\n\u003c?php\n$posts = Post::model()-\u003efindAll();\n```\n\nonly the records that fullfill the `beforeRead()` condition will be returned.\n\nIn the same way any `$post-\u003esave()` will fail, if `beforeWrite()` returns `false`.\n\n### Override query restriction\n\nBut what if you want to query for all records, e.g. for an admin panel you may ask.\nYou can use the `unrestricted()` scope for this:\n\n\n```php\n\u003c?php\n$posts = Post::model()-\u003eunrestricted()-\u003efindAll();\n```\n\n\u003e **Note:** If you did another (restricted) query before, the restriction condition will\n\u003e be applied to the internal model criteria. To reset any potential scopes, you could\n\u003e either call `resetScope()` or pass `true` as argument to `unrestricted()`.\n\n### Override write restriction\n\nFor write operations you can call the `force()` method before you write. This will bypass\nthe `beforeWrite()` check and always save the record. For convenience it returns the same\nrecord, so you can easily chain your calls:\n\n```php\n\u003c?php\n$post-\u003eforce()-\u003esave();\n```\n\n\u003e **Note:** Validation rules are still applied. So if your record has validation errors,\n\u003e it will not be saved, even if you called `force()` before.\n\n### Disable automatic query and write restriction\n\nYou can also disable the automatic access restriction and only do restricted queries\nand writes on explicit demand. To do so you need to set `enableRestriction` to false in the\nbehavior configuration.\n\nYou then can apply the restriction query condition through a named scope:\n\n```php\n\u003c?php\n$posts = Post::model()-\u003ereadable()-\u003efindAll();\n```\n\nFor write operations you'd use the `writeable()` constraint:\n\n```php\n\u003c?php\n$post-\u003ewriteable()-\u003esave();\n```\n\n## Limitations\n\nDue to the limitations in the ActiveRecord implementation, the constraints from this\nbehavior are not applied when you use one of the following methods:\n\n * `deleteAll()`\n * `saveAttributes()`\n * `saveCounters()`\n * `findBySql()`\n * `findAllBySql()`\n * `countBySql()`\n * `exists()`\n * `updateByPk()`\n * `updateAll()`\n * `updateCounters()`\n * `deleteByPk()`\n * `deleteAll()`\n * `deleteAllByAttributes()`\n\nWe recommend to avoid the above methods, or only use them if you're sure about the\nimplications.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Faccessrestrictable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodemix%2Faccessrestrictable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Faccessrestrictable/lists"}