From c7003f5c80460391dde362329dc1eca854657baa Mon Sep 17 00:00:00 2001 From: scs Date: Tue, 23 Jul 2019 14:38:44 +0000 Subject: [PATCH] Add role functions --- main.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/main.go b/main.go index 999d023..0ab1899 100644 --- a/main.go +++ b/main.go @@ -190,6 +190,48 @@ func Bump(username string) { 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) { var username, email string 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 } + func SetRecoveryTemplate(t string) bool { if len(t) != 0 { r, err := template.New("recovery").Parse(t)