• Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Roles is an authorization library for Golang

Roles

Roles is an authorization library for Golang, it also integrates nicely with QOR Admin.

GoDoc Build Status

Usage

Permission Modes

Permission modes are really the roles in Roles. Roles has 5 default permission modes:

  • roles.Read
  • roles.Update
  • roles.Create
  • roles.Delete
  • roles.CRUD // CRUD means Read, Update, Create, Delete

You can use those permission modes, or create your own by defining permissions.

Permission Behaviors and Interactions

  1. All roles in the Deny mapping for a permission mode are immediately denied without reference to the Allow mapping for that permission mode.

    E.g.

    roles.Deny(roles.Delete, roles.Anyone).Allow(roles.Delete, "admin")

    will deny access to admin for the permission mode roles.Delete, despite the chained call to Allow(). I.e. Allow() has NO effect in this chain.

  2. If there are NO roles in the Allow mapping for a permission mode, then roles.Anyone is allowed.

    E.g.

    roles.Deny(roles.CRUD, "customer")

    will allow access for permission mode roles.CRUD to any role that is not a customer because the Allow mapping is empty and the blanket allow rule is in force.

  3. If even one (1) Allow mapping exists, then only roles on that list will be allowed through.

    E.g.

    roles.Allow(roles.READ, "admin")

    allows the admin role through and rejects ALL other roles.

The following is a flow diagram for a specific permission mode, e.g. roles.READ.

st=>start: Input role
denied0=>end: Denied
allowed0=>end: Allowed
denied1=>end: Denied
allowed1=>end: Allowed
op0=>operation: Exists in Deny map?
op1=>operation: Allow map empty?
op2=>operation: Exists in Allow map?
cond0=>condition: Yes or No?
cond1=>condition: Yes or No?
cond2=>condition: Yes or No?

st->op0->cond0
cond0(no)->op1->cond1
cond0(yes)->denied0
cond1(yes)->allowed0
cond1(no)->op2->cond2
cond2(yes)->allowed1
cond2(no)->denied1

Please note that, when using Roles with L10n. The

// allows the admin role through and rejects ALL other roles.
roles.Allow(roles.READ, "admin")

might be invalid because L10n defined a permission system that applys new roles to the current user. For example, There is a user with role "manager", the EditableLocales in the L10n permission system returns true in current locale. Then this user actually has two roles "manager" and "locale_admin". because L10n set resource.Permission.Allow(roles.CRUD, "locale_admin") to the resource. So the user could access this resource by the role "locale_admin".

So you either use Deny instead which means swtich "white list" to "black list" or make the EditableLocales always return blank array which means disabled L10n permission system.

Define Permission

import "github.com/qor/roles"

func main() {
  // Allow Permission
  permission := roles.Allow(roles.Read, "admin") // `admin` has `Read` permission, `admin` is a role name

  // Deny Permission
  permission := roles.Deny(roles.Create, "user") // `user` has no `Create` permission

  // Using Chain
  permission := roles.Allow(roles.CRUD, "admin").Allow(roles.Read, "visitor") // `admin` has `CRUD` permissions, `visitor` only has `Read` permission
  permission := roles.Allow(roles.CRUD, "admin").Deny(roles.Update, "user") // `admin` has `CRUD` permissions, `user` doesn't has `Update` permission

  // roles `Anyone` means for anyone
  permission := roles.Deny(roles.Update, roles.Anyone) // no one has update permission
}

Check Permission

import "github.com/qor/roles"

func main() {
  permission := roles.Allow(roles.CRUD, "admin").Deny(roles.Create, "manager").Allow(roles.Read, "visitor")

  // check if role `admin` has the Read permission
  permission.HasPermission(roles.Read, "admin")     // => true

  // check if role `admin` has the Create permission
  permission.HasPermission(roles.Create, "admin")     // => true

  // check if role `user` has the Read permission
  permission.HasPermission(roles.Read, "user")     // => true

  // check if role `user` has the Create permission
  permission.HasPermission(roles.Create, "user")     // => false

  // check if role `visitor` has the Read permission
  permission.HasPermission(roles.Read, "user")     // => true

  // check if role `visitor` has the Create permission
  permission.HasPermission(roles.Create, "user")     // => false

  // Check with multiple roles
  // check if role `admin` or `user` has the Create permission
  permission.HasPermission(roles.Create, "admin", "user")     // => true
}

Register Roles

When checking permissions, you will need to know current user's roles first. This could quickly get out of hand if you have defined many roles based on lots of conditions - so Roles provides some helper methods to make it easier:

import "github.com/qor/roles"

func main() {
  // Register roles based on some conditions
  roles.Register("admin", func(req *http.Request, currentUser interface{}) bool {
      return req.RemoteAddr == "127.0.0.1" || (currentUser.(*User) != nil && currentUser.(*User).Role == "admin")
  })

  roles.Register("user", func(req *http.Request, currentUser interface{}) bool {
    return currentUser.(*User) != nil
  })

  roles.Register("visitor", func(req *http.Request, currentUser interface{}) bool {
    return currentUser.(*User) == nil
  })

  // Get roles from a user
  matchedRoles := roles.MatchedRoles(httpRequest, user) // []string{"user", "admin"}

  // Check if role `user` or `admin` has Read permission
  permission.HasPermission(roles.Read, matchedRoles...)
}

License

Released under the MIT License.

More Repositories

1

qor

QOR is a set of libraries written in Go that abstracts common features needed for business applications, CMSs, and E-commerce systems.
Go
5,198
star
2

qor-example

An example application showcasing the QOR SDK
Go
1,241
star
3

admin

Qor Admin - Instantly create a beautiful, cross platform, configurable Admin Interface and API for managing your data in minutes.
JavaScript
898
star
4

auth

Golang Authentication solution
Go
677
star
5

transition

Transition is a Golang state machine implementation
Go
425
star
6

validations

Validations is a GORM extension, used to validate models when creating, updating
Go
128
star
7

i18n

I18n is a golang implementation, provides internationalization support for your application, with different backends support
Go
105
star
8

worker

Worker run jobs in background at scheduled time
Go
61
star
9

media

Media add uploading files to cloud or other destinations with support for image cropping and resizing features to any structs
JavaScript
60
star
10

oss

QOR OSS provides common interface to operate files in cloud storage/filesystem
Go
57
star
11

amazon-pay-sdk-go

Amazon Pay Go SDK
Go
31
star
12

mailer

Mail solution
Go
23
star
13

audited

Audited is used to log last UpdatedBy and CreatedBy for your models
Go
21
star
14

auth_themes

Auth Themes
Go
20
star
15

media_library

Abandoned, use https://github.com/qor/media instead
Go
20
star
16

bindatafs

Compile QOR templates into binary with go-bindata
Go
19
star
17

l10n

L10n make your resources(models) be able to localize into different locales
Go
18
star
18

exchange

QOR exchange provides conversion (import/export) functionality for any Qor.Resource to CSV, Excel file
Go
17
star
19

gomerchant

Stripe, Paygent, and Amazon Pay Adaptors
Go
17
star
20

render

Render Templates
Go
16
star
21

seo

SEO module for QOR3
Go
14
star
22

sorting

Sorting: adds sorting and reordering abilities to your models.
JavaScript
12
star
23

publish2

Version Control with Schedule
Go
11
star
24

widget

Qor Widget - Define some customizable, shareable HTML widgets for different pages
Go
11
star
25

session

Session management
Go
8
star
26

filebox

Filebox could be used to provide access permission control for files, directories
Go
8
star
27

doc

QOR3 documentation
CSS
6
star
28

publish

Publish allow user update a resource but do not show the change in website until it is get "published" for GORM-backend models
Go
6
star
29

activity

Qor Activity: add Comment and Track data/state changes to any Qor Resource support to admin interface
Go
6
star
30

notification

QOR Notification
Go
6
star
31

slug

Slug is an extension for qor.
JavaScript
6
star
32

responder

Responder: Respond differently according to request's accepted mime type
Go
6
star
33

location

Qor Location - Make your struct support pick up location from google map in Qor Admin
JavaScript
5
star
34

assetfs

AssetFileSystem
Go
5
star
35

middlewares

Middlewares Management
Go
5
star
36

serializable_meta

Serializable meta
Go
4
star
37

action_bar

Action Bar in QOR3
Go
4
star
38

cache

Cache Store
Go
4
star
39

redirect_back

A Golang HTTP Handler that redirect back to last URL saved in session
Go
4
star
40

page_builder

Page Builder
JavaScript
3
star
41

metas

Meta Types for Admin
JavaScript
3
star
42

log

Qor Logger
Go
3
star
43

help

Help for QOR ADMIN
JavaScript
3
star
44

banner_editor

Banner Editor in QOR3
JavaScript
2
star
45

application

Application
Go
2
star
46

qor-example-cases

Learn QOR3 by examples
Go
2
star
47

wildcard_router

WildcardRouter handles dynamic routes
Go
2
star
48

variations

Variations
JavaScript
2
star
49

app

App Generator - WIP
Swift
1
star