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
2eea4794
Unverified
Commit
2eea4794
authored
May 18, 2020
by
anton troyanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for custom table name
parent
7af908d5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
3 deletions
+24
-3
adapter.go
adapter.go
+24
-3
No files found.
adapter.go
View file @
2eea4794
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"github.com/casbin/casbin/v2/persist"
"github.com/casbin/casbin/v2/persist"
"github.com/go-pg/pg/v9"
"github.com/go-pg/pg/v9"
"github.com/go-pg/pg/v9/orm"
"github.com/go-pg/pg/v9/orm"
"github.com/go-pg/pg/v9/types"
"github.com/mmcloughlin/meow"
"github.com/mmcloughlin/meow"
)
)
...
@@ -30,10 +31,13 @@ type Filter struct {
...
@@ -30,10 +31,13 @@ type Filter struct {
// Adapter represents the github.com/go-pg/pg adapter for policy storage.
// Adapter represents the github.com/go-pg/pg adapter for policy storage.
type
Adapter
struct
{
type
Adapter
struct
{
db
*
pg
.
DB
db
*
pg
.
DB
filtered
bool
tableName
string
filtered
bool
}
}
type
Option
func
(
a
*
Adapter
)
// NewAdapter is the constructor for Adapter.
// NewAdapter is the constructor for Adapter.
// arg should be a PostgreS URL string or of type *pg.Options
// 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
// The adapter will create a DB named "casbin" if it doesn't exist
...
@@ -54,14 +58,31 @@ func NewAdapter(arg interface{}) (*Adapter, error) {
...
@@ -54,14 +58,31 @@ func NewAdapter(arg interface{}) (*Adapter, error) {
// NewAdapterByDB creates new Adapter by using existing DB connection
// NewAdapterByDB creates new Adapter by using existing DB connection
// creates table from CasbinRule struct if it doesn't exist
// 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
}
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
{
if
err
:=
a
.
createTableifNotExists
();
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"pgadapter.NewAdapter: %v"
,
err
)
return
nil
,
fmt
.
Errorf
(
"pgadapter.NewAdapter: %v"
,
err
)
}
}
return
a
,
nil
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
)
{
func
createCasbinDatabase
(
arg
interface
{})
(
*
pg
.
DB
,
error
)
{
var
opts
*
pg
.
Options
var
opts
*
pg
.
Options
var
err
error
var
err
error
...
...
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