admin only route is working

This commit is contained in:
2023-03-29 19:52:46 +11:00
parent 1654ff87ed
commit cc4a890064
7 changed files with 127 additions and 10 deletions

View File

@@ -16,6 +16,13 @@ type User struct {
AccessToken string `db:"AccessToken"`
}
type UserRole struct {
User
RoleName string `db:"RoleName"`
ReadOnly bool `db:"ReadOnly"`
Admin bool `db:"Admin"`
}
func (u *User) SaveUser() (*User, error) {
var err error
@@ -81,7 +88,7 @@ func GetUserByID(uid uint) (User, error) {
var u User
// Query database for matching user object
err := db.QueryRowx("SELECT * FROM Users WHERE UserId=?", uid).StructScan(&u)
err := db.QueryRowx("SELECT * FROM users INNER JOIN roles ON users.RoleId = roles.RoleId WHERE UserId=?", uid).StructScan(&u)
if err != nil {
return u, errors.New("user not found")
}
@@ -96,6 +103,22 @@ func GetUserByID(uid uint) (User, error) {
}
func GetUserRoleByID(uid uint) (UserRole, error) {
var ur UserRole
// Query database for matching user object
fmt.Printf("GetUserRoleByID querying for userid '%d'\n", uid)
err := db.QueryRowx("SELECT users.UserId, users.RoleId, users.UserName, users.Password, users.AccessToken, roles.RoleName, roles.ReadOnly, roles.Admin FROM users INNER JOIN roles ON users.RoleId = roles.RoleId WHERE users.UserId=?", uid).StructScan(&ur)
if err != nil {
fmt.Printf("GetUserRoleByID received error when querying database : '%s'\n", err)
return ur, errors.New("GetUserRoleByID user not found")
}
return ur, nil
}
func (u *User) PrepareGive() {
u.Password = ""
}