SlideShare uma empresa Scribd logo
1 de 15
Content Driven ACL
in the Model Layer
Jeroen Keppens
e-mail
jeroen@amazium.com
twitter
@amazium
blog
http://blog.amazium.com
Use Case

• News site that serves articles, some are free
  others not. You need to be a subscriber to
  access them, or buy access to the article.

• Users can be either visitors (registered, but no
  subscription) or subscribers

• When an article is opened in the browser, the
  article needs to be displayed if the user can
  view it, otherwise show intro and BUY button.
Configuring ACL
 Defining roles, resources & rules
App_Acl_Acl

class App_Acl_Acl extends Zend_Acl
{
    public function __construct()
    {
        // Roles
        $this->addRole('visitor');
        $this->addRole('subscriber', 'visitor');
        
        // Resourcess
        $this->addResource('article');
        $this->addResource('free-article', 'article');
        $this->addResource('charged-article', 'article');
        
        // Access rules
        $this->allow('subscriber', 'article');
        $this->allow('visitor', 'free-article');
        $this->allow('visitor', 'charged-article', null, 
                     new App_Acl_Assert_UserOwned());
    }
}
App_Acl_Assert_UserOwned
class App_Acl_Assert_UserOwned implements Zend_Acl_Assert_Interface
{
    public function assert(Zend_Acl $acl, 
                           Zend_Acl_Role_Interface $role = null, 
                           Zend_Acl_Resource_Interface $resource = null, 
                           $privilege = null)
    {   
        // First we need a good Resource type
        if (!$resource instanceof App_Model_UserOwnedInterface) {
            throw new Exception('UserOwnedInterface not implemented');
        }
        
        // Secondly, we need a authenticated user
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            return false;
        }
        $user = new App_Model_User($auth->getIdentity());
        
        // Then do the check
        /** @var App_Model_UserOwnedInterface $resource */
        return $resource->isOwnedByUser($user);
    }
}
Model Layer
   Domain Logic
User Model

• Implements Zend_Acl_Role_Interface

• User can have a visitor or subscriber role

• User has bought (owns) 0-n articles

• 2 important functions:

      getRoleId() - user's role

      hasArticle() - did the user buy the article?
App_Model_User

class App_Model_User implements Zend_Acl_Role_Interface
{
    /* ... */

    // Implementing Zend_Acl_Role_Interface
    public function getRoleId() 
    {
        return $this->_roleId;
    }

    // Check if a user has bought an article    
    public function hasArticle($article)
    {
        if ($article instanceof App_Model_Article) {
            $article = $article->getId();
        }
        return in_array($article, $this->getUserArticles());
    }
}
Article Model

• Implements Zend_Acl_Resource_Interface

• Implements App_Model_UserOwnedInterface

• Article can be free-article or charged-article

• 2 important functions:

      getResourceId() - article's resource id

      isOwnedByUser() - article bought by user?
App_Model_Article
interface App_Model_UserOwnedInterface
{
    public function isOwnedByUser(App_Model_User $user);
}

class App_Model_Article implements Zend_Acl_Resource_Interface, 
                                   App_Model_UserOwnedInterface
{
    /* ... */

    // Implementing Zend_Acl_Resource_Interface
    public function getResourceId()
    {
        if ($this->isFree()) {
            return 'free-article';
        } else {
            return 'charged-article';
        }
    }

    // Implementing App_Model_UserOwnedInterface
    public function isOwnedByUser(App_Model_User $user)
    {
        return $user->hasArticle($this);
    }
}
Checking ACL
Can the role access the resource?
for ($j = 0; $j < count($users); $j++) {
    // Make ZF think he has authenticated a user
    Zend_Auth::getInstance()->getStorage()->write($users[$j]->toArray());
    // Loop over the articles and check access
    for ($i = 0; $i < count($articles); $i++) {
        $access[] = array(
            'user'    => $users[$j]->getUsername(),
            'article' => $articles[$i]->getTitle(),
            'allowed' => $acl->isAllowed($users[$j], $articles[$i]) 
        );
    }
}




       • Authenticate user

       • Call isAllowed() for
         user & article
More info?

          Read the article below on my blog
                blog.amazium.com

            “Content-driven Access Control
                        with Zend ACL”
http://www.amazium.com/blog/content-driven-access-control-with-zend-acl
Thanks for listening
Please rate the talk at http://joind.in/talk/view/

Mais conteúdo relacionado

Mais procurados

Filters in AngularJS
Filters in AngularJSFilters in AngularJS
Filters in AngularJS
Brajesh Yadav
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 

Mais procurados (20)

Flask and Angular: An approach to build robust platforms
Flask and Angular:  An approach to build robust platformsFlask and Angular:  An approach to build robust platforms
Flask and Angular: An approach to build robust platforms
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
 
Codegnitorppt
CodegnitorpptCodegnitorppt
Codegnitorppt
 
Let ColdFusion ORM do the work for you!
Let ColdFusion ORM do the work for you!Let ColdFusion ORM do the work for you!
Let ColdFusion ORM do the work for you!
 
Extending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh PollockExtending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh Pollock
 
Power of Simplicity in FW/1
Power of Simplicity in FW/1Power of Simplicity in FW/1
Power of Simplicity in FW/1
 
Extend sdk
Extend sdkExtend sdk
Extend sdk
 
Introduction to plugin development
Introduction to plugin developmentIntroduction to plugin development
Introduction to plugin development
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
AngularJS $http Interceptors (Explanation and Examples)
AngularJS $http Interceptors (Explanation and Examples)AngularJS $http Interceptors (Explanation and Examples)
AngularJS $http Interceptors (Explanation and Examples)
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
AngularJS - $http & $resource Services
AngularJS - $http & $resource ServicesAngularJS - $http & $resource Services
AngularJS - $http & $resource Services
 
Filters in AngularJS
Filters in AngularJSFilters in AngularJS
Filters in AngularJS
 
Getting Started-with-Laravel
Getting Started-with-LaravelGetting Started-with-Laravel
Getting Started-with-Laravel
 
ACL in CodeIgniter
ACL in CodeIgniterACL in CodeIgniter
ACL in CodeIgniter
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Let's write secure Drupal code! - DrupalCamp Oslo, 2018Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
 
Angular JS blog tutorial
Angular JS blog tutorialAngular JS blog tutorial
Angular JS blog tutorial
 
Boston Computing Review - Java Server Pages
Boston Computing Review - Java Server PagesBoston Computing Review - Java Server Pages
Boston Computing Review - Java Server Pages
 

Semelhante a Content Driven Zend_Acl in the Model Layer

Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)
Oro Inc.
 

Semelhante a Content Driven Zend_Acl in the Model Layer (20)

Get things done with Yii - quickly build webapplications
Get things done with Yii - quickly build webapplicationsGet things done with Yii - quickly build webapplications
Get things done with Yii - quickly build webapplications
 
Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
 
Yii Introduction
Yii IntroductionYii Introduction
Yii Introduction
 
KAAccessControl
KAAccessControlKAAccessControl
KAAccessControl
 
Android architecture
Android architecture Android architecture
Android architecture
 
First Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentFirst Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven Development
 
前后端mvc经验 - webrebuild 2011 session
前后端mvc经验 - webrebuild 2011 session前后端mvc经验 - webrebuild 2011 session
前后端mvc经验 - webrebuild 2011 session
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
A resource oriented framework using the DI/AOP/REST triangle
A resource oriented framework using the DI/AOP/REST triangleA resource oriented framework using the DI/AOP/REST triangle
A resource oriented framework using the DI/AOP/REST triangle
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For Managers
 
Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)
 
Migrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mindMigrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mind
 
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
 
Solid angular
Solid angularSolid angular
Solid angular
 
Creating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JSCreating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JS
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
 
CodeIgniter 101 Tutorial
CodeIgniter 101 TutorialCodeIgniter 101 Tutorial
CodeIgniter 101 Tutorial
 
Modern android development
Modern android developmentModern android development
Modern android development
 
Angular js
Angular jsAngular js
Angular js
 
Les24
Les24Les24
Les24
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

Content Driven Zend_Acl in the Model Layer

Notas do Editor