Author Archives: Geneva Sibanda

About Geneva Sibanda

I assist companies in the Areas of Network Infrastructure Design and Implementation: (from Windows Active Directory Security, networking, etc.).

Is a Single `role` Column Enough for User Roles in MySQL?

# Is a Single `role` Column Enough for User Roles in MySQL?

Have you ever added a `role` column to your `users` table to manage access to your application, only to realise later that your users might need more than one role?

This is a common starting point when building authentication and authorisation systems. You create a users table, add a `role` column, and assign each user a role such as `admin`, `manager`, or `worker`.

At first, everything seems straightforward. But as your application grows, this design can become restrictive.

Let’s look at an example using a real MySQL users table.

1. Understanding the current users table

First, let’s inspect the structure of our existing `users` table:

DESC users;

The output shows that our table contains several fields, including:

id
first_name
last_name
email
cellphone
country
password
role
created_at
updated_at
status
approved_at
approved_by
deleted_at

The important field for this discussion is:

role varchar(20) NOT NULL

This means every user must have a role value, and the column can store a single string of up to 20 characters.

There is nothing inherently wrong with this approach if every user will always have exactly one role.

However, what happens when a user needs to be both a manager and an administrator?

A single `role` column cannot properly represent that relationship.

2. Checking which roles currently exist

Before changing the database structure, let’s find out which roles are already being used.

We can use the `DISTINCT` keyword:

SELECT DISTINCT role FROM users;

Our output is:

+———+
| role |
+———+
| admin |
| manager |
| worker |
+———+
3 rows in set

We currently have three distinct roles:

– `admin`
– `manager`
– `worker`

This confirms that our application currently recognises three role values.

3. Counting users assigned to each role

Next, let’s find out how many users belong to each role.

SELECT role, COUNT(*) AS total FROM users GROUP BY role;

The result is:

+———+——-+
| role | total |
+———+——-+
| admin | 1 |
| manager | 1 |
| worker | 1 |
+———+——-+

At the moment, each role has one user assigned to it.

This query is particularly useful when managing applications with many users because it gives you a quick overview of how users are distributed across different roles.

4. Viewing users and their assigned roles

We can also retrieve individual users together with their assigned roles:

SELECT id, first_name, last_name, email, role FROM users;

Our current output looks like this:

+—-+————+———–+———————–+———+
| id | first_name | last_name | email | role |
+—-+————+———–+———————–+———+
| 1 | j | 2 | j2@bigfarms.com | admin |
| 2 | j | 3 | j3@bigfarms.com | manager |
| 3 | j | 4 | j4@bigfarms.com | worker |
+—-+————+———–+———————–+———+

As you can see, each user has exactly one assigned role.

We can also retrieve users with a particular role. For example, to find all administrators:

SELECT * FROM users WHERE role = ‘admin’;

This works perfectly well with the current database design.

The limitation only becomes apparent when a user needs multiple roles.

5. The problem with storing multiple roles in one column

Imagine that one of your managers is also responsible for administering the application.

You might be tempted to store both roles in the same column:

admin,manager

Although MySQL allows this because the column is a `VARCHAR`, it introduces problems.

For example:

– Searching for users with a particular role becomes more complicated.
– Enforcing valid role names becomes difficult.
– You cannot easily establish a foreign key relationship between individual roles and users.
– Managing permissions becomes harder.
– Reporting and filtering become less reliable.

A better database design is to separate users and roles.

6. A better approach: Create a roles table

Instead of storing role names directly inside the `users` table, we can create a dedicated `roles` table.

CREATE TABLE roles (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE
);

We can then populate it with our existing roles:

INSERT INTO roles (name)
VALUES
(‘admin’),
(‘manager’),
(‘worker’);

Next, we create a junction table called `user_roles`.

CREATE TABLE user_roles (
user_id INT UNSIGNED NOT NULL,
role_id INT UNSIGNED NOT NULL,

PRIMARY KEY (user_id, role_id),

FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (role_id) REFERENCES roles(id)
);

This table connects users to roles.

The relationship is now many-to-many:

– One user can have multiple roles.
– One role can belong to multiple users.

For example, a user who is both an administrator and a manager would have two records in `user_roles`.

This is much more flexible than storing multiple role names in a single column.

7. Taking permissions a step further

There is another important consideration when designing role-based access control.

A role such as `admin` or `manager` does not automatically explain what a user is allowed to do.

For example, should a manager be allowed to:

– Create users?
– Approve workers?
– Edit records?
– Delete records?
– View reports?

One approach is to introduce a dedicated `permissions` table.

CREATE TABLE permissions (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
);

We can then create a `role_permissions` junction table:

CREATE TABLE role_permissions (
role_id INT UNSIGNED NOT NULL,
permission_id INT UNSIGNED NOT NULL,

PRIMARY KEY (role_id, permission_id),

FOREIGN KEY (role_id) REFERENCES roles(id),
FOREIGN KEY (permission_id) REFERENCES permissions(id)
);

Now each role can have multiple permissions, and each permission can be assigned to multiple roles.

For example:

| Role | Permission |
| — | — |
| admin | create\_user |
| admin | delete\_user |
| manager | approve\_worker |
| manager | view\_reports |
| worker | view\_assigned\_tasks |

The advantage is that permissions are no longer tightly coupled to role names in your application code.

You can change which permissions belong to a role by updating database records rather than rewriting role-specific permission logic.

## Conclusion

Our current `users` table works because each user has one role: `admin`, `manager`, or `worker`.

However, as an application grows, users may need to perform responsibilities that span multiple roles.

When that happens, a single `role` column becomes restrictive.

Separating roles into their own table and introducing a `user_roles` junction table gives your application a more flexible and scalable foundation.

Adding a permissions system takes the design further by allowing you to control exactly what each role can do.

**The takeaway:** Whenever you design a database column that stores a category, role, tag, or type, ask yourself whether a record might eventually need more than one value. If the answer is yes, consider modelling that relationship with separate tables from the beginning.

Until next time\!

One implementation note: Your existing `users.role` column should not be dropped immediately when migrating to this design. First, create the new tables, migrate the existing `admin`, `manager`, and `worker` assignments into `roles` and `user_roles`, update your application code, and verify everything before removing the old column.

Unable to call mysql command line with credentials from mysql_clientdb.conf

Checking ISPConfig database .. mysqlcheck: Got error: 1698: Access denied for user ‘root’@’localhost’ when trying to connect
OK
ERROR 1698 (28000): Access denied for user ‘root’@’localhost’
Unable to call mysql command line with credentials from mysql_clientdb.conf

Solution:

cat /usr/local/ispconfig/server/lib/mysql_clientdb.conf

nano /usr/local/ispconfig/server/lib/mysql_clientdb.conf

mysql -u root -p‘;

?>

How to configure cisco switch for pppoe connection

Configuring a Cisco device (typically a router or layer 3 switch acting as a CPE) for PPPoE involves creating a Dialer interface, setting authentication (CHAP/PAP), enabling encapsulation, and binding it to a physical WAN port. Key steps include defining pppoe-client dial-pool-number, setting mtu 1492, and using ip address negotiated. 

Configuration Steps

Configure the Physical Interface (e.g., Gig0/0) connected to the Fibre ONT:

interface GigabitEthernet0/0
no ip address
pppoe-client dial-pool-number 1
no shutdown

Configure the Dialer Interface (e.g., Dialer1):

interface Dialer1
ip address negotiated
encapsulation ppp
dialer pool 1
mtu 1492
ppp authentication chap callin
ppp chap hostname
ppp chap password
ppp pap sent-username password

Configure Routing to send internet traffic through the Dialer interface:

ip route 0.0.0.0 0.0.0.0 Dialer1

Verification Commands

show pppoe session: Confirms the session is active.

show ip interface brief: Checks if the Dialer1 interface received an IP address.

show ip route: Verifies the default route is pointing to the Dialer interface

How to set Favicon Icon in Codeigniter 4

You can put the favicon.ico file in the web root…./public folder, so the URL would be yourdomain.com/favicon.ico

Then you have to add the following line of code into your HTML:

I have used this in my code layout.php and its working fine!

Class “MySQLi” not found: in

Class “MySQLi” not found: in /web/system/library/db/mysqli.php on line 29

How to solve “Fatal error: Class ‘MySQLi’ not found”: in?

Solution:

root@en-03:~# apt install php8.2-mysql

How to upgrade debian 11 to 12

Step-by-Step Guide to Upgrade to Debian 12 from Debian 11

Copy & Paste as below:

sed -i ‘s/bullseye/bookworm/g’ /etc/apt/sources.list
sed -i ‘s/bullseye/bookworm/g’ /etc/apt/sources.list.d/*.list

apt-get update -y && apt-get upgrade –without-new-pkgs -y && apt-get full-upgrade -y

reboot
lsb_release -a

Whoops! We seem to have hit a snag. Please try again later

After installing CodeIgniter 4 when you run the app then you will get an error like this:

Whoops! We seem to have hit a snag. Please try again later

And in cmd line when run composer update you see something similar to this output below:
Problem 1
– codeigniter4/framework[4.0.0, …, v4.4.4] require ext-intl * -> it is missing from your system. Install or enable PHP’s intl extension.
– Root composer.json requires codeigniter4/framework ^4.0 -> satisfiable by codeigniter4/framework[4.0.0, …, v4.4.4].

To enable extensions, verify that they are enabled in your .ini files:
– /etc/php/8.2/cli/php.ini
– /etc/php/8.2/cli/conf.d/10-mysqlnd.ini
– /etc/php/8.2/cli/conf.d/10-opcache.ini
– /etc/php/8.2/cli/conf.d/10-pdo.ini
– /etc/php/8.2/cli/conf.d/15-xml.ini
– /etc/php/8.2/cli/conf.d/20-calendar.ini
– /etc/php/8.2/cli/conf.d/20-ctype.ini
– /etc/php/8.2/cli/conf.d/20-curl.ini
– /etc/php/8.2/cli/conf.d/20-dom.ini
– /etc/php/8.2/cli/conf.d/20-exif.ini
– /etc/php/8.2/cli/conf.d/20-ffi.ini
– /etc/php/8.2/cli/conf.d/20-fileinfo.ini
– /etc/php/8.2/cli/conf.d/20-ftp.ini
– /etc/php/8.2/cli/conf.d/20-gd.ini
– /etc/php/8.2/cli/conf.d/20-gettext.ini
– /etc/php/8.2/cli/conf.d/20-iconv.ini
– /etc/php/8.2/cli/conf.d/20-mbstring.ini
– /etc/php/8.2/cli/conf.d/20-mysqli.ini
– /etc/php/8.2/cli/conf.d/20-pdo_mysql.ini
– /etc/php/8.2/cli/conf.d/20-phar.ini
– /etc/php/8.2/cli/conf.d/20-posix.ini
– /etc/php/8.2/cli/conf.d/20-readline.ini
– /etc/php/8.2/cli/conf.d/20-shmop.ini
– /etc/php/8.2/cli/conf.d/20-simplexml.ini
– /etc/php/8.2/cli/conf.d/20-sockets.ini
– /etc/php/8.2/cli/conf.d/20-sysvmsg.ini
– /etc/php/8.2/cli/conf.d/20-sysvsem.ini
– /etc/php/8.2/cli/conf.d/20-sysvshm.ini
– /etc/php/8.2/cli/conf.d/20-tokenizer.ini
– /etc/php/8.2/cli/conf.d/20-xmlreader.ini
– /etc/php/8.2/cli/conf.d/20-xmlwriter.ini
– /etc/php/8.2/cli/conf.d/20-xsl.ini
You can also run `php –ini` in a terminal to see which files are used by PHP in CLI mode.
Alternatively, you can run Composer with `–ignore-platform-req=ext-intl` to temporarily ignore these required extensions.

The solution: This method works better and doesn’t have the message above.

root@en-03:# sudo apt-get install php8.2-mcrypt php8.2-intl php8.2-curl php8.2-xsl php8.2-mbstring php8.2-xsl php8.2-zip php8.2-soap php8.2-gd

root@en-03:# sudo service nginx restart
root@en-03:# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@en-03:#

Debian 12 The system will suspend now!

You have just installed Debian 12. However SSH session receive this unexpected message:

Broadcast message from Debian-gdm@en-03 on tty1 (Mon 2024-01-08 09:34:01 SAST):

The system will suspend now!

The solution: This method works better to prevent SSH disconnecting from client side.

Create /etc/systemd/sleep.conf.d/nosuspend.conf as follows:

root@en-03:~# mkdir -p /etc/systemd/sleep.conf.d/

root@en-03:~# vi /etc/systemd/sleep.conf.d/nosuspend.conf

[Sleep]
AllowSuspend=no
AllowHibernation=no
AllowSuspendThenHibernate=no
AllowHybridSleep=no

:wq

How to check codeigniter 4 version

If you don’t know what version of CodeIgniter you are currently running, and assuming that Composer was used to install CodeIgniter: you can get it by running the following command line:

genie@en-03:/var/www/busylines.co.za/web$composer show | grep codeigniter
codeigniter4/framework v4.4.3 The CodeIgniter framework v4

The current version is v4.4.3.

All the best of luck.