Over-engineering a permissions system

March 14, 2009 – 8:58 pm

As before, I’m still working on a WebManager library / framework / toolbox, and a next step in that is to improve the system’s authorization system. From my understanding, this works roughly as follows:

In WebManager, as described in my previous post, a WCB is made out of several Components, such as a form, an element, etcetera. Each component (although ‘each’ isn’t a fact, fyi) has, next to a basic set of settings, also an optional setting for permissions. With permissions, I refer to access control lists (of sorts) for the five (or so) basic usergroups that exist in WebManager, usergroups such as authors, users, developers, etcetera. Each usergroup can have a set of permssions assigned to them, which indicate what that user can do. These permissions can then, in the controller or in a .JSP file, be checked again using WebManager’s AuthorizationService, a service object with some permission checking methods.

A permission is defined as follows. First, you create a Category, basically a parent for each permission. The Category is defined as a Category instance, which gets passed a ‘category’ string, an unique identifier for that category. Usually, at least in the default archetypes, this identifier is equal to the component’s bundle ID, which in turn is a combination of the WCB’s ID and an appendage (such as ‘element’ or whatnot). This results in a category string similar to, for example, “info.greatjustice.wcb.somewcb.element”.

Next, one creates a set of Permission objects, which are objects created according to a permission string (a should-be unique string) and the category defined before. The permission string is usually the category string, with the permission itself appended to it (such as ‘.update’, ‘.delete’, etc). This leads to a permission string of ‘info.greatjustice.wcb.somewcb.element.update’, for example.

Now that there’s a set of permissions (we’ll use the base set of CRUD permissions as an example here - create, read, update, delete), we can assign them to the usergroups that have these permissions. When no permissions are defined, all users have full access to the WCB component. When just one permission is set in one usergroup, that usergroup can only perform that action, nothing else. Anyways, to assign a permission to a permission group, the ComponentDefinition object, which is used to configure and (by WebManager itself) initialize a WCB’s component, has a method, setPermissionsForPermissionGroup(), that allows the setting of a permission. The method takes two arguments, the usergroup (which is a constant string defined in WebManager’s WCBConstants class) and an array of Permission objects to assign to that usergroup.

Next to that, the Element component type has a few extra methods (setCreatePermission and setDeletePermission) that, I assume, sets a global permission that indicates that component can be created and deleted, or in this case, can be inserted or removed from a page - regardless of user rank, i.e. ‘global’.

Now, back to the topic at hand and my own work on this, I figured that this system wasn’t ideal, for a number of reasons.

First, there’s an enormous amount of freedom associated with setting a permission. Because you can pass strings, there’s no way of knowing whether it’ll ‘work’ until you try it out. There’s no set specification about what input is allowed, and nor is there any checking for proper values. This leads me to believe that the whole permission system in WebManager is little more than a string comparison system (as in, if the usergroup identified by this string has the permission defined by that string, return true, else return false), which is fine in itself, but then, why’s there the complexity in the assigning of the permissions? Why bother creating a long permission string when you could just fill in “Slartibartfast” as a permission, and check if a user has the “Slartibartfast” permission all the time?

I can understand that, of course, since I’m sure the system would allow checking for a permission set in component A in component B, with this relatively low-level approach. Still though, I believed that this system could be improved. The lowest step, the assigning of the permission category is pretty much the starting point. I don’t believe that that could be abstracted any more, at least not without completely automating the system of assigning permissions, so that’ll largely stay the same.

The permissions themselves though could be largely automated. There’s a few facts we know about the permissions:

  • The permission string is always (or, 99.9% of the time) based on the category’s string.
  • The permission is in most cases one of Create, Read, Update, or Delete, and in exceptional cases it’s a similar system with other permissions such as ‘UpdateSomePart’.
  • The permission is never used in a dynamic sense - once a permission has been created, it doesn’t turn into something else.
  • The usergroups are a pre-defined set of 5 groups, currently defined as plain strings.

Now, with these above facts in mind, there’s a number of things we could do to improve on the permission system.

  • Add typesafe constraints to the permissions and usergroups
  • Auto-generate permissions based on sensible defaults
  • Auto-generate permissions based on what a user wants

Note that there’s one or two additional points to the above list, but they apply to the entire library in a wider sense of the word - i.e. an alternative / easier method of creating ComponentPermissions. I’ll do a full article on that once it’s finished.

We’ll want to improve on the permission system for a number of reasons. First, we’ll want to make it more typesafe - if you know there’s a limited set of usergroups (5, in this case), why would you want to have the users set it as a string? If you know a permission is always a fixed value, why set it as a string value that allows everyone to freely fill things in?

Second, we’ll want to automate - in several layers of meaning - the process of creating and assigning permissions. If you know the category and know the set of permission types, it’s easy to auto-generate the Permission objects and assign them to the ComponentDefinition automatically. Automating in ’several layers’ mean, in this case, that there’s the possiblity to have all permissions auto-generate (the base CRUD permissions, for example), but at the same time not restrict the users in defining their permissions.

After a few days of development (or something, I don’t really keep track of time - I don’t seem to have any deadline or other important tasks where I work), The end-product consisted of a set of components that allow for easy and typesafe definition of permissions, with several layers of automation and customization.

One of the first things I did was to put the permission groups into an enum, a language feature I’ve never used until about 9 months ago, but which I already love. Using an enum instead of the constant string values defined in WebManager’s WCBConsts has the main advantage of being typesafe - when you have a method that expects a usergroup, you can only accept usergroups, instead of having to do a manual check to examine the string value passed to it. The setPermissionsForPermissionGroup could, if WebManager used the enum approach, accept a permission group and an array of Permission objects instead of ’some’ string and an array. This guarantees that there is no way that a user could ever enter the wrong value, simply because the software wouldn’t compile if there’s an incorrect value set there.

The second thing I did was to make my own subclass of the PermissionCategory object, which, at first, just defined a constructor that forced each mandatory value to be set at object create-time - a PermissionCategory requires two or three values in order to function properly, but those values could only be set using setter methods. The custom permsision category defined a single constructor that forces the user to define all two or three values in one go, so it becomes impossible to ‘forget’ a value. This approach is also used in the ComponentDefinition replacement code in the same library, but as said, more on that in a later article.

Later, the custom PermissionCategory got a few additional methods that created Permission objects. A Permission always has a Category as its parent, so why not invert it and have a Category spawn its Permissions?

As said earlier, a Permission’s permission string is usually the same as the Category’s permission string, so why not base a Permission’s string on the Category’s string with just a small appendage (’.create’, for example)?

In this approach, I once again returned to the enum. Defined a Permissions enum that defined four permissions: Create, Update, Read, and Delete. Each had a string representation as well defined inside the enum, string representations in the form of  ’.create’, ‘.update’, ‘.read’, etcetera.

The permissions enum, in combination with the custom PermissionCategory, resulted in a ‘createPermission’ method in the PermissionCategory that took just one parameter, an entry from the Permission enum. The result was a concatenation of the category’s string and the Permission’s toString() method result, which was then inserted into a new Permission object, along with the category assigned to it that created it.

Next to a Permission enum, I also created a PermissionGroup enum, which acted a simple wrapper around the string values from WebManager itself - only then typesafe. With it came some extra methods that allow a developer to get the groups ‘above’ a current group, so they could, for example, allow everything for the Editor usergroup and all those above it.

So, with an abstraction of the Permissions, the PermissionGroup, and the PermissionCategory in place, we’ve already come a long way. We can now create a set of permissions for a permission group and, using another abstraction for the creation of ComponentDefinitions, assign them all in one go. However, we can go one step further, by auto-generating the permissions.

In most cases, there’s just a single set of permissions that you’d always apply - CRUD. Create, Read, Update, and Delete, and this often in relation to an object of sorts - in this case, an Element component, or the object that you can insert into a page, can only be created (for example) by an Editor, but can only be deleted again by the Main Editor usergroup, which is one level up in the hierarchy.

WebManager’s permission system allows for more fine-grained permissions, but in most cases, this default set of permissions would work good enough. So, I also added an abstraction for storing and auto-generating CRUD permissions. This was done in two stages, actually. First up was a ‘generic’ CRUDPermissionsContainer, an interface that defines four methods for getting the four permissions. I wanted to allow people to define their ‘own’ CRUD permissions quartet instead of forcing them to either use the auto-generated ones or have to manually define all permissions, hence the abstraction. Underneath that are two default implementations: A CRUDPermissionsContainer implementation that works only with the earlier defined CRUDPermissions enum, which took just a PermissionCategory as constructor argument and created the permissions from there automatically, and a more generic container with a constructor with five arguments, the PermissionCategory object itself, and four Permission objects - one for each of the four CRUD permissions.

As the title suggests, this is a bit over-engineered, but it’s good practice material - I’m still cheap for my employer, so I can spend some more time on over-engineering. I know that there’s plenty of people that wish they had some excess time to spend on refactoring, =D.

But anyways, to wrap things up. My total permission system setup now allows users to set up the complete permission set for all users in a single line of code, and allows full customization in far less lines of code than the old one (where the old one involved at least two or three dozen lines - creating five string values,  create a couple of Permission objects, put those in arrays, call the same method a couple of times in the ComponentDefinition object to assign them, etcetera. Having to do that every time for each component / WCB that requires permissions gets tedious, and not to mention it increases the code and, as such, the complexity of a class / method, and reduces its readability.

I’ll post a bit of code later on (maybe) to further illustrate this post, one line of code can say more than a thousand words and whatnot.

Tags: ,

GX WebManager’s WCB Dependency Management

March 1, 2009 – 1:54 pm

It’s been far too long since I posted on here, so after an upgrade to WP 2.7 (or something), here’s a new post. The title may be a tad confusing, so I’ll try to explain that (and the abbreviations) first now.

I’m currently doing a 20-week internship at Oberon Interactive, a largely web-oriented company that does webdesign, online campaigns, that kinda stuff, all from their HQ in Amsterdam. It’s got about 12 or so employees (including myself), but I’m not sure yet because there’s almost always someone missing. On there, I’ve been assigned with the task of making development of WCB’s easier.

Before I throw in more TLA’s though, I’ll explain what I’m working with. I’m working with a pretty extensive enterprise content management system called WebManager, made by Dutch company GX. Since WebManager is the only piece of software GX seems to be developing, I’ll probably often confuse WebManager with GX and vice-versa, so if I use GX in reference to an item, I mean WebManager.

Anyways, since version 9 (I believe) of WebManager, GX has added the ability for developers to add functionality to WebManager through a plug-in-like system. This functionality is packaged in bundles called WebManager Content Bundles, aka WCB’s. These WCB’s are basically OSGi modules, built on top of Apache Felix, an implementation of OSGi’s service platform.

Now, Oberon has received at least one assignment from a large Dutch football site (i.e. the football type with the round ball, not the oblong one) that, at least for now, involves creating two of these WCB’s to be included into their site. The visible bit of a WCB can easily be inserted into existing WebManager pages through WebManager’s Edit Environment, a back-end that looks an awful lot like Microsoft Word.

Anyways, I’m working with that currently, and my primary internship assignment is to make WCB development a lot easier, reliable, and faster. There’s several aspects of WCB development that have to be improved, as I’ve discovered / determined for myself so far, and this article is about one of them: Dependency management. I’m sure I’ll be writing more posts during my internship on a range of (WCB development) related subjects, so be sure to hang around.

For this first installment, I’ve taken a closer look on how the WCB API’s handle dependency management, also comparing it with other, more formalized approaches. I’ve put the report into a neat, 10-page Word document, which I’ll link to below. Here’s a summary though, and you’re going to have to visit the internets to figure the terms I use out or read the .doc itself in order to find out what is what.

A WCB is built out of separate Components, like a Form, an Element, a Servlet, etc. Each Component has one central class of sorts that is initialized by some obscure part of WebManager, i.e. out of the programmer’s control. In order to be able to use, for example, the Authorization Service, with which you can check access permissions for users for a certain access, you have to pass a string value containing the class name of said Authorization Service to a new instance of a ServiceDependency object, then pass that to a ComponentDefinition object which is passed back to WebManager when your WCB is installed / initialized.

There’s loads of problems with this approach:

* No typesafe checking, so you’ll only see if you’ve entered the wrong classname at runtime (whilst one could pass a Class object, much more typesafe).

* It’s hard to get to the central object to get the service(s), you either have to call a getComponent() method from the controller class and cast that into the specific class, or implement the class as a singleton.

* No control over the dependencies’ lifecycle - you can only pass the class you’d like to initialize to WebManager, can’t pass any parameters or whatever.

* You can only inject services into that one Component object.

* Injection is (usually) done using reflection on a class’s private fields, obliterating the meaning of the ‘private’ keyword, and preventing the usage of setter methods (and additional behaviour one could associate with that).

All in all, they do use the term ‘dependency injection’ a few times in their official documentation, but in practice all you do is pass the names of the classes you’d like to have created, have WebManager inject those into an object that acts as a hard-to-access Service Locator, and be done with it. The Service Locator isn’t a bad choice per sé, but since the dependencies contained in the SL are injected somewhere at runtime, after all the objects (Controller, for example) are created, you’d only be able to access the dependencies at runtime of an object, through accessor methods. I’d much prefer if an object’s dependencies were injected at construction-time, through constructor injection, etc.

But all that, and then some, is all described in the document. Read it and let me know what you think - I’d love a second opinion on the matter, or alternative solutions to solving this problem.

http://greatjustice.info/files/dependency-management.doc

Edit: Small update, if this topic interests you, make sure to read Fowler’s article on the subject - it helped me formalize the different techniques used. Also, I should seriously buy / read his book(s), fgj.

Tags: ,

Using Bitmasks in a Groups Permission System

October 1, 2008 – 8:00 pm

As promised in my previous post, this article will describe a practical application of bitmasks, in this particular case for usage in a permission system on a webapplication. This particular application is a social network site, in which a user can create a joinable group. Each group has four usergroups - Guests, Members, Moderators, and Administrators, and as a special group with no specific traits of its own, Creator(s) (i.e. the person(s) that created the group).

Each usergroup has a set of permissions in each group - so basically, you have to be able to store a usergroup’s permissions on a per-group basis. In this article, I’ll explain how I’ve used bitmasks and the associated operators as explained in the previous article to store those permissions, as well as the usergroup(s) a particular user belongs to in a specifc group. The bottom line is that by storing the permission a usergroup has in a bitmask, you end up with a lot less database usage. But that’ll be explained in this article.

Read the rest of this entry »

Tags: ,

The Lost Art of Bitmasks

September 30, 2008 – 8:00 pm

In this article, I will attempt to explain the so-called bitmasks, and the application of the binary operators (such as &, |, << and >>) found in pretty much every programming language. I myself have been in my education for four years now, but only last year or so did I encounter a practical application for using them. The title is as such perhaps just personal, seeing that I don’t really know the level of most modern-day programmers or whether they’re familiar with binary operations, but in my own experience, more emphasis is put on high-level structures and techniques, compared to low-level data storage, management and manipulation. This article will provide an introduction to binary operators, as well as how and when they can be applied - in a basic fashion, I’m sure that a lot more advanced applications for these techniques can be thought up.

Read the rest of this entry »

Tags:




Zend Framework

July 29, 2008 – 1:24 pm

There are numerous web application frameworks written in and for PHP on the market nowadays, most of them free and/or open source, most of them following or supporting the MVC paradigm. This summer, I’m going to go and attempt to make a website using one of the newer frameworks out there: the Zend Framework. Made (or at least started / supported by) the people / company that also does the PHP language itself, it promises a solid framework for web application development, ‘extreme simplicity and productivity’ (from the website), up-to-date with the latest internet technologies, etcetera.

In this post (and following posts), I will dive into Zend Framework, describe its various loosely-coupled components, and make an attempt at judging the framework.

Read the rest of this entry »

Tags: , ,

Applied design patterns: C4 Engine Messages

July 15, 2008 – 5:00 pm

In my previous article about the C4 engine, I highlighted what was, in my opinion, the biggest downside of the C4 engine: its demo game. Or more precisely, the code and design of the demo game. Because I had to get points for university and didn’t have time to dive into said code and refactor it all, I wrote a refactoring proposal (found here: Office ‘97-’03 / Office ‘07) and put it on the C4 Engine forums. After that, and after the project, I still had to get some points, so I executed some of the refactorings.

So far, most refactorings I’ve done involve moving functionality from one central Game class to different smaller classes - resource and game management so far. One refactoring however is one I’m pretty proud of (in all decency), and is described in the refactoring proposal as the Composite Message System.

In this article, I’ll (attemt to) explain the process of this particular refactoring: explain the old situation, what the problem was with that situation, how it was analyzed and how, eventually (and with the help of my trusty Design Patterns book) I came up with a replacement system of that situation, one that was a lot more flexible and reusable, and one that would accellerate the speed at which programmers could produce results.

For those that are interested, I’ll be discussing the Composite, Visitor, Factory Method and (to a lesser degree) Builder design patterns in this article.

Read the rest of this entry »

Tags: , , , ,

The C4 Engine

July 14, 2008 – 10:06 am

During the last 20 weeks (February to June ‘08), I and 7 other people have been working on a game project (both for university points and for this online group of dudes that started the creation of this particular game), 2 of them programmers, 5 of them artsy fags media students (who did models, textures, sketches, sound/music etc).

The game itself is a sci-fi tactical first person shooter (well, it’s supposed to be in a distant future, olz), which was basically in a half-assed concept stage for about a year or two, during which some very nice concept art drawings were made, but nothing concrete was made, such as actual game design (i.e. functional design) or background story.

The project was eventually put forward to our university as a project for the Game Design minor, and since then, we’ve been working on it for the last 20 weeks. In this post, I won’t go into detail about the project itself, but more into the 3D engine that was used: The C4 engine. Read the rest of this entry »

Tags: , , ,

University versus self-taught

June 22, 2008 – 2:15 am

A topic I’ve been thinking about for quite some time now. If I was an employer, looking for an employee, which would I take: The type that basically self-taught himself programming through reading books and/or websites, or the type that spend an X amount of years at university and has a bachelor’s or master’s degree? Let’s assume that, when they apply, they both have roughly the same amount of knowledge and/or skill.

I’ll unveil it right now: I’d totally go for the one that went to university and got a degree. Why? I’ll explain (or, attempt to.) Read the rest of this post for a wider view of my opinion, as well as a massive edit written when this post got a relatively large amount of attention quite suddenly and unsuspectingly.

Read the rest of this entry »

Applied design patterns: Database abstraction (part 2)

June 9, 2008 – 8:21 pm

In some of my previous posts, I’ve pretty much ranted about the poor design qualities of some people that enjoy calling themselves PHP programmers. In these two posts however, I’d like to show you what good and proper design is. The topic is the same as that in my previous posts: databases. Or, to be more precise, database abstraction.

In the first part, transformed a concrete set of database interaction classes into an abstract set, to improve flexibility and extensibility. In the second part (i.e. this one), we’ll introduce a design pattern which will help us to manage and maintain the new abstract database classes and their concrete implementations. In this post, we’ll create and implement the design pattern we’ll use to determine which concrete database connection should be used, using an implementation of the Factory Method.

Read the rest of this entry »

Tags: , ,

Applied design patterns: Database abstraction (part 1)

June 9, 2008 – 8:21 pm

In some of my previous posts, I’ve pretty much ranted about the poor design qualities of some people that enjoy calling themselves PHP programmers. In these two posts however, I’d like to show you what good and proper design is. The topic is the same as that in my previous posts: databases. Or, to be more precise, database abstraction.

In the first part, we’ll transform a concrete set of database interaction classes into an abstract set, to improve flexibility and extensibility. In the second part, we’ll introduce a design pattern which will help us to manage and maintain the new abstract database classes and their concrete implementations. We’ll do part one in this post.

Read the rest of this entry »

Tags: , ,