LiveUserPEAR::LiveUser authentication and permission framework |
[ AdvancedSearch | AreaMap ]
|
| Welcome to LiveUser | Documentation | RoadMap | Wishlist | About |
| Documentation | FAQ | References | Tutorials | Other |
|
Areas In LiveUser |
This document is supposed to be the Wiki version of Arnaud's LiveUser tutorial and to be considered work in progress. It will be updated to reflect the latest LiveUser release while converting it to the wiki.. As long as this disclaimer exists, many things explained in this document can be considered outdated!!! About this documentAuthorsAt the time of this version, Arnaud Limbourg wrote this document. With help from Markus Wolff, Jean-Marc Fontaine, Bjoern Kraus and others on LiveUser mailing-list. Abbreviations used throughout the document
Intended audienceWho should read this document ? If you are planning on using LiveUser (LU) in your application and wonder how to get on with it then this document is for you. If you are a developer wishing to help with the development of LU or are wondering how to extend LU features then this document is not for you, this will be part of some other documentation. What is LiveUserLiveUser purpose is to provide a flexible and extensible framework to be used for authentication and authorization schemes. It does not aim at being the “One-true-solution” but rather providing the means to achieve what you need. Rationale: What is authentication and what are permissions Authentication is a simple concept. It consists of checking if “you-are-who-you-say-you-are”. Of course, there are different ways to implement that idea. The one which interests us is based on the classic login/password mechanism. Permissions are simple as well. You want to make sure somebody is allowed to perform a given action. Of course, there are many ways to implement a permission handling mechanism. Where to find LiveUserLiveUser resides in PEAR. To install LiveUser you need a running PEAR environment. More information on PEAR can be found at http://pear.php.net/ Once it is up and running you can launch the following command:
You probably want some additional modules, including:
This command will download the latest version of the LiveUser package from the PEAR server and install it in the PEAR directory on your system. LiveUser DesignLiveUser design is based on containers. A container is similar in concept to a driver. Take a printer, you know what a printer is but every model behaves differently from the others. Thus you need a specific driver which works for your printer. Containers are the same, to handle a specific authentication scheme you need a container. The design is based on two sets of containers. One set is used for authentication and the other for permissions. Bundled with the package are several ready-made containers which answer common problems encountered in real-life applications. Remember that if there isn't one that suits your needs, just write a new container ! Liveuser is not a single-sign-on system (yet !) but you can build one with it. Directory layout and database schema are provided in the appendices 1 and 2. Rationale: Why containers ? This approach gives you a great flexibility. If bundled containers do not fit your needs, you can just add your own. Extending LiveUser is not part of this document. Rights ManagementAll rights are saved into some kind of data storage container (the type of that container - i.e. flat files, RDBMS, LDAP or others - depends on what type of auth/perm container you have chosen). Each right has an integer ID and a name for easy idenfitication. A script is bundled with LiveUser which will read the database and generate a PHP file. This file will contain one constant for every available right. You can then include this file in your application and use the constants when you check for a right. This approach saves you the hassle of remembering that right id 1 is to edit news, you just use a constant named EDITNEWS. It also helps you when you´re moving an application using LiveUser from one server to another: If some other application on the new server already uses the right id´s that you used on the old server, simply run the generator script again and everything´s fine. Note: the defineGenerator script only generates a php file for a DB based container. XML based or other containers are not treated by it. Depending on what container you plan to use you will choose a different option for constants naming. The following chapter will give you hints on what to choose. The LiveUser classThe LiveUser class (formerly known as LoginManager? in early LiveUser versions) is the class developer will use to for authentication/permission checking. Examples are to login/logout users and check for a specific right. This, along with the administration classes, is the only class in the package that you will use directly. However, this is not how it must be done every time. If you have some running application that is already using another authentication package, for example PEAR::AUTH, you always have the option of using the permission container classes directly to add permission handling – they all can be used standalone, if you wish. But using LiveUser class is a lot more comfortable. Note: there is an experimental PEAR::Auth container in CVS. LiveUser out-of-the-boxBundled containersLiveUser comes with bundled containers. Two sets are provided:
The names are pretty straightforward, DB means the container uses PEAR::DB, MDB obviously uses PEAR::MDB (which makes both containers database-centric) while XML... well, is XML-file based. What container to choose is dependent on what you want to achieve. For a simple application or website where you don't have many users and don't want to bother the XML based containers might just do the trick. If you prefer a DB based mechanism and don't have that many users DB.php for authentication and DB_Simple for permissions should be fine. For busier applications/websites you may be looking at using DB_Medium or DB_Complex. You may be wondering what are the differences between Simple, Medium and Complex, and rightly so! The differences lie in how the permissions are handled. Note: Many people already have an existing user database. LiveUser can be customized to specify which table/fields to use. It means that you do not have to transfer all the users to a specific schema but you can use your own. Following is a list that shows what is implemented by which containers:
To take advantage of the Levelsystem you have to store ownership of all «Objects» to which you want to control access via LiveUser. The beauty of all this is, that even though the mechanisms for retrieving the rights differ (which leads to performance differences between the containers – DB_Simple being the fastest, DB_Complex the slowest) the API to your application remains the same regardless of which container you actually use. Because of that, you can easily switch container types without having to touch even one line of code in your application – just edit some lines in the configuration file and you´re done. As for the three different DB containers provided with LiveUser, even the database models are compatible – therefore, DB_Simple can use the full model of DB_Complex (some tables simply won´t be used) and if one day it should become necessary to implement a more complex approach for administering permissions, just switch the container type to DB_Medium or DB_Complex and you´re done. Using rightsPrevious chapters hinted how the choice of a container will affect the generation of a php file with constants for every right. The following list should give you a pretty comprehensive view about the matter:
Of course, it is up to you to choose the way that suits you most. If this all looks too abstract I recommend reading "A sample applicatoin" and the appendices (all see below :-)) A sample applicationWe will build a news management application to show how to use LiveUser. A full online-demo will be available at some point (no date can be given, there was a demo version almost ready but changes in LiveUser which happened in-between forces to update it). The specifications of the application are simple: John Doe wants a web application which allows him to publish news on a web page. He wants to give access to anyone he wants on particular news sections. There are three sections, namely: "General", "World" and "Gaming". As it is the first application he is making, he does not want to bother too much. He understands what a right is and wants a simple way of giving rights to a user. Having to think about groups/subgroups gives him a headache so he wants to keep it pretty basic. He designed the application in three parts, one will show the news and is accessible to anyone, the other is restricted to given users. The third part is restricted to John Doe himself and is where he manages user accounts. Having read the documentation, John Doe knows that choosing an authentication container is not of paramount imoprtance to get his hands dirty. He also knows that he has to choose a permission container before starting. John Doe does not feel very well today so he chooses the DB_Simple container which should cause him very few headaches. He decides to start hacking the main page which will welcome visitors and show them the news. There is no authentication needed on this page, everybody has access to it. He just wants to have a login form displayed on the screen for contributors. In order to do his work he read some literature and feels that separating the display and the control is a good thing. So he installs PEAR::DB as a database access layer and HTML_Template_IT to display information. He comes up with the following template: /(insert screenshot here)/ He obtained it using the following two files
That took him about two hours to get this up and running. He feels quite excited though at the same time knowing that this was the easy part. He now needs to tackle the administration part. He already decided to have one script called admin.php which will handle it all. He starts by entering one user and three rights. Being a bit lazy John Doe will use the provided admin classes, the following script should help. In this example Auth_DB and Perm_DB_Medium are used but every container charing the same API you can interchange them.
AppendicesAppendix 1: Directory Layout/TODO/ Appendix 2: Database/TODO/ Appendix 3: How to implement this in my application?Many users may ask how to put this in their application without writing this piece of code on every page. This would make any change a nightmare. It is not the purpose of this document to talk about application design issues. The following non-exhaustive list should give you general hints:
The major drawback is that it puts authentication automatically on every page.
This solution gives you a good flexibility. While providing a standard behaviour it leaves you a satisfactory level control. You can push it further, adding a parameter to give a specific configuration file for example. If you need complete control instantiating the class is probably what you are looking for. Appendix 4: index.php code
Appendix 5: index.tpl code
This site powered by YaWiki 0.22 beta. |