Role Based Access Control (RBAC) Go package with database persistence
Tags: go, webdev, github, rbac
Published: 2021-05-26 15:42:02

[Authority](https://github.com/harranali/authority) is a Golang package that lets you manage roles and permissions, it stores the information in database using [gorm](https://github.com/go-gorm/gorm), once you create roles and their permissions you can assign them to the users of your app using their IDs.

## Features:
Here is a list of all the features of the package 
- Create Roles
- Create Permissions
- Assign Permissions to Roles
- Assign Multiple Roles to Users
- Check User's Roles
- Check User's Permissions
- Check Role's Permissions
- Revoke User's Roles
- Revoke User's Permissions
- Revoke Role's permissions
- List User's Roles
- List All Roles
- List All Permissions
- Delete Roles
- Delete Permissions

## Install
Here is how you can install the package
```go
go get github.com/harranali/authority
```
#### Install the database driver 
[Authority](https://github.com/harranali/authority) uses `gorm` to work with the database, that's why you need to install `gorm` database driver of the database system that you intend to use, here is how you can install each driver
```go
# mysql 
go get gorm.io/driver/mysql 

# or postgres
go get gorm.io/driver/postgres

# or sqlite
go get gorm.io/driver/sqlite

# or sqlserver
go get gorm.io/driver/sqlserver

# or clickhouse
go get gorm.io/driver/clickhouse
```

## Initializing the package
`Authority` creates 4 database tables to store the roles, permissions and their relationships, to initialize `authority` you need to pass two parameters, the first one is the prefix of the table names, the second one is an instance of `gorm`, here is how you can initiate the package 
```go
// initiate the database (using mysql)
dsn := "dbuser:dbpassword@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})

// initiate authority
auth := authority.New(authority.Options{
    TablesPrefix: "authority_",
    DB:           db,
})
```
#### Migrating the database tables 
The package uses `gorm` auto migration, so there is no need to migrate or manage the database tables since it's taken care of for you automatically

## Using the package 
Here are some of the operation that you can perform with the package, to see all the operation please refer to the [authority docs](https://github.com/harranali/authority#docs)

#### Creating roles
Here is how you can create and store roles in the database
```go
err := auth.CreateRole("role-1")
err = auth.CreateRole("role-2")
```

#### Creating permissions
Here is how you can create and store permissions in the database
```go
err := auth.CreatePermission("permission-1")
err = auth.CreatePermission("permission-2")
err = auth.CreatePermission("permission-3")
```
#### Assigning permissions to roles
Here is how you can assign a group of permissions to a given role
```go
err := auth.AssignPermissions("role-1", []string{
    "permission-1",
    "permission-2",
    "permission-3",
})
```

#### Assigning roles to users
Here is how you can assign a role to a user, you can simply pass the user ID as the first parameter and the role as the second parameter
```go
err = auth.AssignRole(1, "role-a") # 1 is the user id
```

#### Checking if a user have a given role 
Here is how you can check if a given user has a particular role, the first argument is the user ID, the second one is the role
```go
ok, err := auth.CheckRole(1, "role-a") # 1 is the user id
```

#### Checking if a user have a given permission 
Here is how you can check if a given user has a particular permission, the first parameter is the user ID, the second one is the permission
```go
ok, err := auth.CheckPermission(1, "permission-d") # 1 is the user id
```

#### Checking role's permissions
Here is how you can check if a given role has a particular permission
```go
ok, err := auth.CheckRolePermission("role-a", "permission-a")
```

#### Revoke user's role
here is how you can revoke roles assigned to users
```go
err = auth.RevokeRole(1, "role-a")
```

#### Retrieve user's roles 
here is how you can get the list of roles assigned to a given user
```go
roles, err := auth.GetUserRoles(1)
```
To see the rest of functionalities please check [authority's docs](https://github.com/harranali/authority#docs).