Documentation

Installation

RoleWarden comes as three zips. Pick the one that matches where you start.

ZipFor
rolewarden-app-x.y.z.zipStarting from nothing, or without Composer: CodeIgniter 4, Shield, CodeIgniter's translations and RoleWarden, with vendor/ included and everything wired.
rolewarden-module-x.y.z.zipAn existing CodeIgniter 4 application that uses Composer.
rolewarden-demo-x.y.z.zipThe demo app on its own (the complete application already contains it).

The complete application

  1. Upload the content of the zip to your server.
  2. Point the web server's document root at the public/ folder. Everything else must stay outside the document root.
  3. Make writable/ writable by the web server, and the application's root folder too if you want the installer to write .env for you.
  4. Create an empty MySQL or MariaDB database and a user for it.
  5. Open the site. The home page leads to the installer.

The browser installer

/rolewarden/install takes the application from an empty database to a first super admin.

  1. Requirements. PHP version, extensions, a writable writable/, and whether .env can be written.
  2. Setup code. The installer saves a one-time code in writable/rolewarden/install-code.txt on the first visit. Open that file on the server (over FTP, for example) and type the code. Only someone who can read the server's files can install, so a stranger who finds a fresh upload first cannot make themselves super admin.
  3. Database. The application's address, then server, port, database name, user and password. The connection is tested; the settings go to .env together with a new encryption key. An existing key is kept, and so is an existing CI_ENVIRONMENT; otherwise it is set to production. When .env cannot be written, the installer shows its content to save by hand. A failed connection never shows database details on screen: they go to writable/logs/.
  4. Tables. The tables of CodeIgniter's Settings library, Shield and RoleWarden, and the system roles.
  5. First super admin. Username, email and password, checked with Shield's password rules. The account is created through Shield and is active.

Then the installer writes writable/rolewarden/installed.lock, deletes the setup code and refuses to run again. It also refuses on an application that already has an active super admin. It never contacts anything outside your server.

Last step: give the application a sender for its security emails, in .env:

email.fromEmail = 'noreply@example.com'
email.fromName = 'Example'
email.protocol = 'smtp'
email.SMTPHost = 'smtp.example.com'
email.SMTPUser = 'noreply@example.com'
email.SMTPPass = 'your-smtp-password'
email.SMTPPort = 587
email.SMTPCrypto = 'tls'

Without email.fromEmail the emails are skipped and a line is written to the log.

Addresses without index.php

CodeIgniter puts index.php/ in the addresses it builds, such as /index.php/rolewarden/users. When the web server sends every request to public/index.php (Apache with mod_rewrite and CodeIgniter's public/.htaccess, or nginx with try_files $uri $uri/ /index.php?$query_string;), add app.indexPage = '' to .env for shorter addresses. Without rewriting, leave it as it is.

Updating the complete application

RoleWarden lives in modules/rolewarden and is loaded from there by app/Config/Autoload.php. To update, replace that folder with the one from the new module zip, then run:

php spark migrate -n RoleWarden

The module in an existing application

Unzip the module zip into your application, for example as modules/rolewarden, and let Composer install it from there with a path repository in your composer.json:

"repositories": [
    { "type": "path", "url": "modules/rolewarden", "options": { "symlink": false } }
],
composer require rolewarden/codeigniter4-rolewarden:^1.0
composer require codeigniter4/translations   # optional: CodeIgniter's own messages in Italian and other languages

Shield must already be installed and set up (php spark shield:setup).

Wiring

In app/Config/Auth.php, let Shield ask RoleWarden for permissions:

public string $userProvider = \RoleWarden\Models\UserModel::class;

In the same file, use RoleWarden's sign-in screen (the $views array) and, if most people who sign in are administrators, land them on the panel (the $redirects array):

'login' => '\RoleWarden\Views\auth\login',
'login' => 'rolewarden/users',

In app/Config/Routes.php, next to the line Shield already needs:

service('auth')->routes($routes);
\RoleWarden\Config\RouteRegistrar::register($routes);

That registers the panel, the installer, and a route serving the panel's CSS and JavaScript from the module: nothing is copied under public/. The module's events (session tracking, security emails, the registration hook) load by themselves as long as app/Config/Modules.php keeps module discovery on for events, the default.

Enable CSRF protection for every form in app/Config/Filters.php (the panel's own routes have it in any case):

public array $globals = [
    'before' => [
        'csrf',
    ],
    // ...
];

Tables, with the installer or by hand

Open /rolewarden/install and follow it (the database step is skipped when your application is already connected), or run the migrations yourself. Run them in separate steps, so that RoleWarden gets a batch of its own and can be rolled back without Shield:

php spark migrate -n 'CodeIgniter\Settings'
php spark migrate -n 'CodeIgniter\Shield'
php spark migrate -n RoleWarden
php spark db:seed 'RoleWarden\Database\Seeds\RoleWardenSeeder'

By hand, the first super admin is a Shield user given the super-admin role once:

php spark shield:user create -n admin -e you@example.com
php spark shield:user activate -e you@example.com
INSERT INTO acl_user_roles (user_id, role_id)
SELECT i.user_id, r.id
FROM auth_identities i
JOIN acl_roles r ON r.slug = 'super-admin'
WHERE i.type = 'email_password' AND i.secret = 'you@example.com';

Replace acl_ if you changed the table prefix (see Configuration).

Updating the module

Replace the module's folder (or run composer update rolewarden/codeigniter4-rolewarden), then php spark migrate -n RoleWarden. A new version brings its changes to existing data as migrations: for example, a new screen comes with a migration that gives the admin role its new permissions.

Removing the module

migrate:rollback undoes whole batches, whatever the namespace. Look up RoleWarden's batch in the migrations table and roll back to the batch before it:

php spark migrate:rollback -b <previous batch>

-b 0 rolls back everything, Shield included. Then remove the wiring lines and the module.

In a subfolder or behind a proxy

In a subfolder

The application can live at an address such as https://example.com/admin/. Point that address at the public/ folder (an alias or a symbolic link) and set app.baseURL in .env to the full address with the subfolder and a final slash. The installer writes it from the address it was opened at. Every link, form and email of the panel is built from app.baseURL.

Behind a reverse proxy or a load balancer

Behind a proxy every request comes from the proxy's own address unless CodeIgniter is told to trust it. The sign-in limit per IP address would then count everyone together and lock everyone out at once, and the activity log, the sessions list and the security emails would show the proxy's address. List your proxies in app/Config/App.php, with the header that carries the visitor's address:

public array $proxyIPs = [
    '10.0.0.5'     => 'X-Forwarded-For',
    '172.16.0.0/12' => 'X-Forwarded-For',
];

When the proxy handles HTTPS and talks to the application over plain HTTP, it must also send X-Forwarded-Proto: https. CodeIgniter reads that header only from the proxies in $proxyIPs. Keep app.baseURL on https://.

The proxy visitors connect to must replace X-Forwarded-For with the visitor's address, not add to what the visitor sent: otherwise anyone can write an address of their choice in the header and get round the sign-in limit per IP address. On nginx:

proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;

On any site served over HTTPS, with a proxy or without, add cookie.secure = true to .env so that the session and "Remember me" cookies are never sent over plain HTTP.

The demo app

A small document register with three roles in a chain of inheritance: a Reader only reads, an Author writes and edits their own documents, an Approver approves and deletes. It shows the permissions at work and is the model to copy for your own screens. The complete application contains it in modules/rolewarden-demo, not installed.

php spark demo:install     # table, permissions, roles, three accounts, sample documents
php spark demo:uninstall   # removes all of it, and nothing else

The accounts are reader@example.com, author@example.com and approver@example.com, with the password set in .env as demo.password (or generated and printed once). Open /demo. In your own application, copy the demo folder and add one line to $psr4 in app/Config/Autoload.php and one to app/Config/Routes.php, as its README.md explains.

Showcase mode, for a public demo or a product video, adds a bar that switches the session between the three demo accounts without a password: demo.showcase = true in .env. It only switches to the accounts the demo created, and it is ignored when CI_ENVIRONMENT is production.