Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
C
casbin-pg-adapter
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abby Cin
casbin-pg-adapter
Commits
d33f9714
Unverified
Commit
d33f9714
authored
May 18, 2020
by
Zixuan Liu
Committed by
GitHub
May 18, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #8 from troyanov/feature/custom-table-name-support
Feature/custom table name support
parents
d951292b
2eea4794
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
12 deletions
+33
-12
adapter.go
adapter.go
+33
-12
No files found.
adapter.go
View file @
d33f9714
...
...
@@ -8,6 +8,7 @@ import (
"github.com/casbin/casbin/v2/persist"
"github.com/go-pg/pg/v9"
"github.com/go-pg/pg/v9/orm"
"github.com/go-pg/pg/v9/types"
"github.com/mmcloughlin/meow"
)
...
...
@@ -31,9 +32,12 @@ type Filter struct {
// Adapter represents the github.com/go-pg/pg adapter for policy storage.
type
Adapter
struct
{
db
*
pg
.
DB
tableName
string
filtered
bool
}
type
Option
func
(
a
*
Adapter
)
// NewAdapter is the constructor for Adapter.
// arg should be a PostgreS URL string or of type *pg.Options
// The adapter will create a DB named "casbin" if it doesn't exist
...
...
@@ -54,14 +58,31 @@ func NewAdapter(arg interface{}) (*Adapter, error) {
// NewAdapterByDB creates new Adapter by using existing DB connection
// creates table from CasbinRule struct if it doesn't exist
func
NewAdapterByDB
(
db
*
pg
.
DB
)
(
*
Adapter
,
error
)
{
func
NewAdapterByDB
(
db
*
pg
.
DB
,
opts
...
Option
)
(
*
Adapter
,
error
)
{
a
:=
&
Adapter
{
db
:
db
}
for
_
,
opt
:=
range
opts
{
opt
(
a
)
}
if
len
(
a
.
tableName
)
>
0
{
a
.
db
.
Model
((
*
CasbinRule
)(
nil
))
.
TableModel
()
.
Table
()
.
Name
=
a
.
tableName
a
.
db
.
Model
((
*
CasbinRule
)(
nil
))
.
TableModel
()
.
Table
()
.
FullName
=
(
types
.
Safe
)(
a
.
tableName
)
a
.
db
.
Model
((
*
CasbinRule
)(
nil
))
.
TableModel
()
.
Table
()
.
FullNameForSelects
=
(
types
.
Safe
)(
a
.
tableName
)
}
if
err
:=
a
.
createTableifNotExists
();
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"pgadapter.NewAdapter: %v"
,
err
)
}
return
a
,
nil
}
// WithTableName can be used to pass custom table name for Casbin rules
func
WithTableName
(
tableName
string
)
Option
{
return
func
(
a
*
Adapter
)
{
a
.
tableName
=
tableName
}
}
func
createCasbinDatabase
(
arg
interface
{})
(
*
pg
.
DB
,
error
)
{
var
opts
*
pg
.
Options
var
err
error
...
...
@@ -145,7 +166,7 @@ func (r *CasbinRule) String() string {
func
(
a
*
Adapter
)
LoadPolicy
(
model
model
.
Model
)
error
{
var
lines
[]
*
CasbinRule
if
_
,
err
:=
a
.
db
.
Query
(
&
lines
,
`SELECT * FROM casbin_rules`
);
err
!=
nil
{
if
err
:=
a
.
db
.
Model
(
&
lines
)
.
Select
(
);
err
!=
nil
{
return
err
}
...
...
@@ -236,7 +257,7 @@ func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
// AddPolicies adds policy rules to the storage.
func
(
a
*
Adapter
)
AddPolicies
(
sec
string
,
ptype
string
,
rules
[][]
string
)
error
{
var
lines
[]
*
CasbinRule
for
_
,
rule
:=
range
rules
{
for
_
,
rule
:=
range
rules
{
line
:=
savePolicyLine
(
ptype
,
rule
)
lines
=
append
(
lines
,
line
)
}
...
...
@@ -261,7 +282,7 @@ func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
// RemovePolicies removes policy rules from the storage.
func
(
a
*
Adapter
)
RemovePolicies
(
sec
string
,
ptype
string
,
rules
[][]
string
)
error
{
var
lines
[]
*
CasbinRule
for
_
,
rule
:=
range
rules
{
for
_
,
rule
:=
range
rules
{
line
:=
savePolicyLine
(
ptype
,
rule
)
lines
=
append
(
lines
,
line
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment