Add role functions
This commit is contained in:
parent
bfb4cd57ff
commit
c7003f5c80
43
main.go
43
main.go
|
@ -190,6 +190,48 @@ func Bump(username string) {
|
||||||
log.Printf("scsusers.Bump: Error on user bump: %s : %s\n", username, err.Error())
|
log.Printf("scsusers.Bump: Error on user bump: %s : %s\n", username, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetRoles(username string) []string {
|
||||||
|
var roles []string
|
||||||
|
q:=fmt.Sprintf(`select role_name from %s_roles
|
||||||
|
left join %s_user_roles on %s_roles.role_id=%s_user_roles.role_id
|
||||||
|
left join %s_auth on %s_user_roles.user_id=%s_auth.user_id
|
||||||
|
where %s_auth.username=?`, c.TablePrefix, c.TablePrefix, c.TablePrefix, c.TablePrefix, c.TablePrefix, c.TablePrefix, c.TablePrefix)
|
||||||
|
err:=c.db.Select(&roles,q, username)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("scsusers.GetRoles: %s : %s\n", username, err.Error())
|
||||||
|
}
|
||||||
|
return roles
|
||||||
|
}
|
||||||
|
|
||||||
|
func HasRole(username string, role string) bool {
|
||||||
|
roles:=GetRoles(username)
|
||||||
|
for _,a:=range(roles) {
|
||||||
|
if a==role {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddRole(username string, role string) bool {
|
||||||
|
if HasRole(username, role) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
q:=fmt.Sprintf(`insert into %s_user_roles (user_id, role_id)
|
||||||
|
VALUES (
|
||||||
|
(select userid from %s_auth where username=?),
|
||||||
|
(select role_id from %s_roles where role_name=?)
|
||||||
|
);
|
||||||
|
`, c.TablePrefix, c.TablePrefix, c.TablePrefix)
|
||||||
|
_, err:=c.db.Exec(q)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("scsusers.AddRole: %s %s %s\n", username, role, err.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func RecoverByUsername(u string) {
|
func RecoverByUsername(u string) {
|
||||||
var username, email string
|
var username, email string
|
||||||
q:=fmt.Sprintf("select username, email from %s_auth where username=$1", c.TablePrefix)
|
q:=fmt.Sprintf("select username, email from %s_auth where username=$1", c.TablePrefix)
|
||||||
|
@ -350,6 +392,7 @@ func SetAlertTemplate(t string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func SetRecoveryTemplate(t string) bool {
|
func SetRecoveryTemplate(t string) bool {
|
||||||
if len(t) != 0 {
|
if len(t) != 0 {
|
||||||
r, err := template.New("recovery").Parse(t)
|
r, err := template.New("recovery").Parse(t)
|
||||||
|
|
Loading…
Reference in New Issue