grim/hgkeeper
Clone
Summary
Browse
Changes
Graph
Update to casbin v2
2019-09-12, Gary Kramlich
b6e0150db76d
Update to casbin v2
package
access
import
(
"path/filepath"
"sync"
log
"github.com/sirupsen/logrus"
)
const
(
modelFilename
=
"model.conf"
policyFilename
=
"policy.csv"
)
var
(
accessLock
sync
.
Mutex
adminRepoPath
string
adminRepoName
string
)
func
Setup
(
reposPath
,
adminRepo
string
)
error
{
adminRepoName
=
adminRepo
adminRepoPath
=
filepath
.
Join
(
reposPath
,
adminRepo
)
return
Refresh
()
}
func
AdminRepo
()
string
{
return
adminRepoName
}
func
AdminRepoPath
()
string
{
return
adminRepoPath
}
// Refresh will try to reload the casbin model and policies followed by SSH
// keys. If there is an error it's possible that the casbin model and polcies
// could have been updated but the ssh keys were not.
func
Refresh
()
error
{
accessLock
.
Lock
()
defer
accessLock
.
Unlock
()
if
err
:=
refreshEnforcer
(
adminRepoPath
);
err
!=
nil
{
return
err
}
if
err
:=
refreshKeys
(
adminRepoPath
);
err
!=
nil
{
return
err
}
return
nil
}
func
check
(
user
,
repo
,
action
string
)
bool
{
r
,
err
:=
enforcer
.
Enforce
(
user
,
repo
,
action
)
if
err
!=
nil
{
log
.
Errorf
(
"failed to authenticate (%q, %q, %q): %v"
,
user
,
repo
,
action
,
err
,
)
return
false
}
return
r
}
func
CanRead
(
user
,
repo
string
)
bool
{
return
check
(
user
,
repo
,
"read"
)
}
func
CanWrite
(
user
,
repo
string
)
bool
{
return
check
(
user
,
repo
,
"write"
)
}
func
CanInit
(
user
,
repo
string
)
bool
{
return
check
(
user
,
repo
,
"init"
)
}