Skip to content

Office Authentication

Andy Theuninck edited this page Apr 6, 2015 · 1 revision

The backend eventually needs access controls. In my opinion, the "backendsecurity" column in the employees table isn't going to work. There's enough going on that tiered access just doesn't make sense. For example, the front end manager should be able to edit cashiers but probably not products. Buyers or scanning should be able to edit products but probably not cashiers.

We need something more nuanced (mandatory access controls if you're into terminology). The system described here is included with fannie. It isn't perfect (frankly, the table structure is just bone-headed) but it works and is reasonably flexible.

Authenticating Users

The auth package is primarily SQL based. The UI lets you add and remove users, change passwords, etc. The system can be run entirely this way with usernames and (hashed) passwords right in the database. Users generally don't like keeping track of lots of passwords, so I've added a couple alternatives.

You can authenticate against an /etc/shadow file. A small utility, shadowread, is included to let apache read password hashes off the local system. PHP's PAM module requires /etc/shadow to be entirely readable. The shadowread utility uses setuid to essentially grep lines out of /etc/shadow. This setup may well have some security drawbacks; make your own decisions on convenience vs security.

You can authenticate against an LDAP server. I use it with openldap. It could probably be done with Active Directory in a Windows environment. Again this is just for name & password matching. PHP's LDAP module is required. The defaults are probably correct for a posixAccount schema. The domain and username fields are combined to build the bind DN. For example, if I enter "corepos" as the user name with username field "uid" and domain "ou=People,dc=example,dc=com", Office will attempt to bind to the LDAP server as "uid=corepos,ou=People,dc=example,dc=com" using the provided password. Anonymous search on the domain base is required although only on the username field at minimum.

Both LDAP and shadow logins will automatically create users in the database if needed. Password related fields will be blank but permissions are still kept in SQL. The LDAP and/or shadow options only validate names & passwords.

Permissions

The auth package is structured around the notion of authorization classes. An authorization class is simply something a user is allowed to do - for instance "edit_prices" could be an authorization class.

For finer granularity, there are optional sub-class limits. These are pretty flexible. These don't have to mean the same thing in all contexts, and can be anything that works in a SQL BETWEEN statements. For example, you could use these to give someone permission to edit prices, but only for products in departments 10 through 15. Or you could give someone permission to edit members, but only if their member type is 1.

Sample data is provided to load existing authorization classes that Office already uses. This data should be loaded automatically when authentication is turned on, but if it's missing it can be manually loaded from the dashboard interface on the Sample Data tab.

Groups

You can put users in groups and then assign authorization classes to the group. The table structure for this is idiotic but functional. It saves some headaches rolling out a new tool or trying to remember all the authorizations a new employee needs.

Sample data is also provided for groups. These groups represent some common role-based permission sets. Sample Data is found via the dashboard interface. It has its own tab.

Using Authentication as a Developer

Classes based on FanniePage can handle many authentication situations very easily using properties of the class:

  • $must_authentication [boolean, default false] - if set to true the user will automatically be prompted to log in
  • $current_user - [string] username is set automatically authentication is required.
  • $auth_classes - [array] of authorization classes permitted on the page. Note: leaving $auth_classes as an empty array with $must_authenticate=true is an acceptable configuration. This means the user must have a valid username and password but does not need any particular permissions beyond that.

For more complex situations, there are really only two functions that get used a lot to access user information:

  • FannieAuth::checkLogin() returns the name of the user, or false if the user has not logged in.
  • FannieAuth::validateUserQuiet(string $auth_class [,string $sub='all']) returns the name of the user if they are logged in and have the requested authorization. Otherwise it returns false.

The login page (auth/ui/loginform.php) accepts an optional GET argument in the URL, redirect. So a fannie tool that uses authentication will have a little section like this:

if (!FannieAuth::validateUserQuiet("edit_prices")){
    $redirect = $FANNIE_URL."item/ItemEditor.php";
    $loginpage = $FANNIE_URL."auth/ui/loginform.php";
    header("Location: $loginpage?redirect=$redirect");
    
    return false;
}

This sends the user to a login form, if needed, and once they enter a valid username and password brings them right back to what they were doing.

Installation

  • In the Authentication section ("tab") of the Office Install page select "Yes" for Authentican Enabled and click "Re-run".
  • After that, if no Users have been entered yet there will be input boxes for Username and Password for an initial one with "admin" authorization. Click "Re-run".
  • These credentials will let you log in to the Authentication interface at FANNIE/auth/ui/ - however other protected pages may still be closed to a user who only has "admin" authorization.

Clone this wiki locally