mirror of
https://github.com/khairul169/vaulterm.git
synced 2026-09-15 17:03:30 +07:00
feat: team access control
This commit is contained in:
@@ -54,7 +54,21 @@ func login(c *fiber.Ctx) error {
|
||||
|
||||
func getUser(c *fiber.Ctx) error {
|
||||
user := utils.GetUser(c)
|
||||
return c.JSON(user)
|
||||
teams := []TeamWithRole{}
|
||||
|
||||
for _, item := range user.Teams {
|
||||
teams = append(teams, TeamWithRole{
|
||||
ID: item.TeamID,
|
||||
Name: item.Team.Name,
|
||||
Icon: item.Team.Icon,
|
||||
Role: item.Role,
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(&GetUserResult{
|
||||
AuthUser: *user,
|
||||
Teams: teams,
|
||||
})
|
||||
}
|
||||
|
||||
func logout(c *fiber.Ctx) error {
|
||||
|
||||
@@ -1,6 +1,20 @@
|
||||
package auth
|
||||
|
||||
import "rul.sh/vaulterm/middleware"
|
||||
|
||||
type LoginSchema struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type TeamWithRole struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Icon string `json:"icon"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
type GetUserResult struct {
|
||||
middleware.AuthUser
|
||||
Teams []TeamWithRole `json:"teams"`
|
||||
}
|
||||
|
||||
@@ -20,8 +20,14 @@ func NewRepository(r *Hosts) *Hosts {
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Hosts) GetAll() ([]*models.Host, error) {
|
||||
query := r.ACL(r.db.Order("id DESC"))
|
||||
func (r *Hosts) GetAll(opt GetAllOpt) ([]*models.Host, error) {
|
||||
query := r.db.Order("id DESC")
|
||||
|
||||
if opt.TeamID != "" {
|
||||
query = query.Where("hosts.team_id = ?", opt.TeamID)
|
||||
} else {
|
||||
query = query.Where("hosts.owner_id = ? AND hosts.team_id IS NULL", r.User.ID)
|
||||
}
|
||||
|
||||
var rows []*models.Host
|
||||
ret := query.Find(&rows)
|
||||
@@ -30,10 +36,8 @@ func (r *Hosts) GetAll() ([]*models.Host, error) {
|
||||
}
|
||||
|
||||
func (r *Hosts) Get(id string) (*models.HostDecrypted, error) {
|
||||
query := r.ACL(r.db)
|
||||
|
||||
var host models.Host
|
||||
ret := query.Joins("Key").Joins("AltKey").Where("hosts.id = ?", id).First(&host)
|
||||
ret := r.db.Joins("Key").Joins("AltKey").Where("hosts.id = ?", id).First(&host)
|
||||
if ret.Error != nil {
|
||||
return nil, ret.Error
|
||||
}
|
||||
@@ -48,13 +52,12 @@ func (r *Hosts) Get(id string) (*models.HostDecrypted, error) {
|
||||
|
||||
func (r *Hosts) Exists(id string) (bool, error) {
|
||||
var count int64
|
||||
ret := r.ACL(r.db.Model(&models.Host{}).Where("id = ?", id)).Count(&count)
|
||||
ret := r.db.Model(&models.Host{}).Where("id = ?", id).Count(&count)
|
||||
return count > 0, ret.Error
|
||||
}
|
||||
|
||||
func (r *Hosts) Delete(id string) error {
|
||||
query := r.ACL(r.db)
|
||||
return query.Delete(&models.Host{Model: models.Model{ID: id}}).Error
|
||||
return r.db.Delete(&models.Host{Model: models.Model{ID: id}}).Error
|
||||
}
|
||||
|
||||
func (r *Hosts) Create(item *models.Host) error {
|
||||
@@ -62,15 +65,5 @@ func (r *Hosts) Create(item *models.Host) error {
|
||||
}
|
||||
|
||||
func (r *Hosts) Update(id string, item *models.Host) error {
|
||||
query := r.ACL(r.db.Where("id = ?", id))
|
||||
|
||||
return query.Updates(item).Error
|
||||
}
|
||||
|
||||
func (r *Hosts) ACL(query *gorm.DB) *gorm.DB {
|
||||
if r.User.IsAdmin {
|
||||
return query
|
||||
}
|
||||
|
||||
return query.Where("hosts.owner_id = ?", r.User.ID)
|
||||
return r.db.Where("id = ?", id).Updates(item).Error
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
@@ -19,10 +20,15 @@ func Router(app fiber.Router) {
|
||||
}
|
||||
|
||||
func getAll(c *fiber.Ctx) error {
|
||||
teamId := c.Query("teamId")
|
||||
user := utils.GetUser(c)
|
||||
repo := NewRepository(&Hosts{User: user})
|
||||
|
||||
rows, err := repo.GetAll()
|
||||
if teamId != "" && !user.IsInTeam(&teamId) {
|
||||
return utils.ResponseError(c, errors.New("no access"), 403)
|
||||
}
|
||||
|
||||
rows, err := repo.GetAll(GetAllOpt{TeamID: teamId})
|
||||
if err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
@@ -41,8 +47,13 @@ func create(c *fiber.Ctx) error {
|
||||
user := utils.GetUser(c)
|
||||
repo := NewRepository(&Hosts{User: user})
|
||||
|
||||
if body.TeamID != nil && !user.TeamCanWrite(body.TeamID) {
|
||||
return utils.ResponseError(c, errors.New("no access"), 403)
|
||||
}
|
||||
|
||||
item := &models.Host{
|
||||
OwnerID: user.ID,
|
||||
OwnerID: &user.ID,
|
||||
TeamID: body.TeamID,
|
||||
Type: body.Type,
|
||||
Label: body.Label,
|
||||
Host: body.Host,
|
||||
@@ -76,13 +87,17 @@ func update(c *fiber.Ctx) error {
|
||||
repo := NewRepository(&Hosts{User: user})
|
||||
|
||||
id := c.Params("id")
|
||||
exist, _ := repo.Exists(id)
|
||||
if !exist {
|
||||
return utils.ResponseError(c, fmt.Errorf("host %s not found", id), 404)
|
||||
data, _ := repo.Get(id)
|
||||
if data == nil {
|
||||
return utils.ResponseError(c, errors.New("host not found"), 404)
|
||||
}
|
||||
if !data.CanWrite(&user.User) || !user.TeamCanWrite(body.TeamID) {
|
||||
return utils.ResponseError(c, errors.New("no access"), 403)
|
||||
}
|
||||
|
||||
item := &models.Host{
|
||||
Model: models.Model{ID: id},
|
||||
TeamID: body.TeamID,
|
||||
Type: body.Type,
|
||||
Label: body.Label,
|
||||
Host: body.Host,
|
||||
@@ -111,9 +126,12 @@ func delete(c *fiber.Ctx) error {
|
||||
repo := NewRepository(&Hosts{User: user})
|
||||
|
||||
id := c.Params("id")
|
||||
exist, _ := repo.Exists(id)
|
||||
if !exist {
|
||||
return utils.ResponseError(c, fmt.Errorf("host %s not found", id), 404)
|
||||
host, _ := repo.Get(id)
|
||||
if host == nil {
|
||||
return utils.ResponseError(c, errors.New("host not found"), 404)
|
||||
}
|
||||
if !host.CanWrite(&user.User) {
|
||||
return utils.ResponseError(c, errors.New("no access"), 403)
|
||||
}
|
||||
|
||||
if err := repo.Delete(id); err != nil {
|
||||
|
||||
@@ -9,7 +9,12 @@ type CreateHostSchema struct {
|
||||
Port int `json:"port"`
|
||||
Metadata datatypes.JSONMap `json:"metadata"`
|
||||
|
||||
TeamID *string `json:"teamId"`
|
||||
ParentID *string `json:"parentId"`
|
||||
KeyID *string `json:"keyId"`
|
||||
AltKeyID *string `json:"altKeyId"`
|
||||
}
|
||||
|
||||
type GetAllOpt struct {
|
||||
TeamID string
|
||||
}
|
||||
|
||||
@@ -20,10 +20,16 @@ func NewRepository(r *Keychains) *Keychains {
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Keychains) GetAll() ([]*models.Keychain, error) {
|
||||
var rows []*models.Keychain
|
||||
query := r.ACL(r.db.Order("created_at DESC"))
|
||||
func (r *Keychains) GetAll(opt GetAllOpt) ([]*models.Keychain, error) {
|
||||
query := r.db.Order("created_at DESC")
|
||||
|
||||
if opt.TeamID != "" {
|
||||
query = query.Where("keychains.team_id = ?", opt.TeamID)
|
||||
} else {
|
||||
query = query.Where("keychains.owner_id = ? AND keychains.team_id IS NULL", r.User.ID)
|
||||
}
|
||||
|
||||
var rows []*models.Keychain
|
||||
ret := query.Find(&rows)
|
||||
return rows, ret.Error
|
||||
}
|
||||
@@ -34,9 +40,7 @@ func (r *Keychains) Create(item *models.Keychain) error {
|
||||
|
||||
func (r *Keychains) Get(id string) (*models.Keychain, error) {
|
||||
var keychain models.Keychain
|
||||
query := r.ACL(r.db.Where("id = ?", id))
|
||||
|
||||
if err := query.First(&keychain).Error; err != nil {
|
||||
if err := r.db.Where("id = ?", id).First(&keychain).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -45,8 +49,7 @@ func (r *Keychains) Get(id string) (*models.Keychain, error) {
|
||||
|
||||
func (r *Keychains) Exists(id string) (bool, error) {
|
||||
var count int64
|
||||
query := r.ACL(r.db.Model(&models.Keychain{}).Where("id = ?", id))
|
||||
ret := query.Count(&count)
|
||||
ret := r.db.Model(&models.Keychain{}).Where("id = ?", id).Count(&count)
|
||||
return count > 0, ret.Error
|
||||
}
|
||||
|
||||
@@ -70,14 +73,5 @@ func (r *Keychains) GetDecrypted(id string) (*KeychainDecrypted, error) {
|
||||
}
|
||||
|
||||
func (r *Keychains) Update(id string, item *models.Keychain) error {
|
||||
query := r.ACL(r.db.Where("id = ?", id))
|
||||
return query.Updates(item).Error
|
||||
}
|
||||
|
||||
func (r *Keychains) ACL(query *gorm.DB) *gorm.DB {
|
||||
if r.User.IsAdmin {
|
||||
return query
|
||||
}
|
||||
|
||||
return query.Where("keychains.owner_id = ?", r.User.ID)
|
||||
return r.db.Where("id = ?", id).Updates(item).Error
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package keychains
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@@ -23,17 +23,22 @@ type GetAllResult struct {
|
||||
}
|
||||
|
||||
func getAll(c *fiber.Ctx) error {
|
||||
teamId := c.Query("teamId")
|
||||
withData := c.Query("withData")
|
||||
|
||||
user := utils.GetUser(c)
|
||||
repo := NewRepository(&Keychains{User: user})
|
||||
|
||||
rows, err := repo.GetAll()
|
||||
if teamId != "" && !user.IsInTeam(&teamId) {
|
||||
return utils.ResponseError(c, errors.New("no access"), 403)
|
||||
}
|
||||
|
||||
rows, err := repo.GetAll(GetAllOpt{TeamID: teamId})
|
||||
if err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
|
||||
if withData != "true" {
|
||||
if withData != "true" || (teamId != "" && !user.TeamCanWrite(&teamId)) {
|
||||
return c.JSON(fiber.Map{"rows": rows})
|
||||
}
|
||||
|
||||
@@ -67,8 +72,13 @@ func create(c *fiber.Ctx) error {
|
||||
user := utils.GetUser(c)
|
||||
repo := NewRepository(&Keychains{User: user})
|
||||
|
||||
if body.TeamID != nil && !user.TeamCanWrite(body.TeamID) {
|
||||
return utils.ResponseError(c, errors.New("no access"), 403)
|
||||
}
|
||||
|
||||
item := &models.Keychain{
|
||||
OwnerID: user.ID,
|
||||
OwnerID: &user.ID,
|
||||
TeamID: body.TeamID,
|
||||
Type: body.Type,
|
||||
Label: body.Label,
|
||||
}
|
||||
@@ -94,15 +104,18 @@ func update(c *fiber.Ctx) error {
|
||||
repo := NewRepository(&Keychains{User: user})
|
||||
|
||||
id := c.Params("id")
|
||||
|
||||
exist, _ := repo.Exists(id)
|
||||
if !exist {
|
||||
return utils.ResponseError(c, fmt.Errorf("key %s not found", id), 404)
|
||||
data, _ := repo.Get(id)
|
||||
if data == nil {
|
||||
return utils.ResponseError(c, errors.New("key not found"), 404)
|
||||
}
|
||||
if !data.CanWrite(&user.User) || !user.TeamCanWrite(body.TeamID) {
|
||||
return utils.ResponseError(c, errors.New("no access"), 403)
|
||||
}
|
||||
|
||||
item := &models.Keychain{
|
||||
Type: body.Type,
|
||||
Label: body.Label,
|
||||
TeamID: body.TeamID,
|
||||
Type: body.Type,
|
||||
Label: body.Label,
|
||||
}
|
||||
|
||||
if err := item.EncryptData(body.Data); err != nil {
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package keychains
|
||||
|
||||
type CreateKeychainSchema struct {
|
||||
Type string `json:"type"`
|
||||
Label string `json:"label"`
|
||||
Data interface{} `json:"data"`
|
||||
TeamID *string `json:"teamId"`
|
||||
Type string `json:"type"`
|
||||
Label string `json:"label"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type GetAllOpt struct {
|
||||
TeamID string
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package teams
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"rul.sh/vaulterm/db"
|
||||
"rul.sh/vaulterm/models"
|
||||
"rul.sh/vaulterm/utils"
|
||||
)
|
||||
|
||||
type Teams struct {
|
||||
db *gorm.DB
|
||||
User *utils.UserContext
|
||||
}
|
||||
|
||||
func NewRepository(r *Teams) *Teams {
|
||||
if r == nil {
|
||||
r = &Teams{}
|
||||
}
|
||||
r.db = db.Get()
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Teams) GetAll() ([]*models.Team, error) {
|
||||
var rows []*models.Team
|
||||
ret := r.db.Order("created_at DESC").Find(&rows)
|
||||
return rows, ret.Error
|
||||
}
|
||||
|
||||
func (r *Teams) Create(data *models.Team) error {
|
||||
return r.db.Create(data).Error
|
||||
}
|
||||
|
||||
func (r *Teams) Get(id string) (*models.Team, error) {
|
||||
var data models.Team
|
||||
if err := r.db.Where("id = ?", id).First(&data).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
func (r *Teams) Exists(id string) (bool, error) {
|
||||
var count int64
|
||||
ret := r.db.Model(&models.Team{}).Where("id = ?", id).Count(&count)
|
||||
return count > 0, ret.Error
|
||||
}
|
||||
|
||||
func (r *Teams) Update(id string, item *models.Team) error {
|
||||
return r.db.Where("id = ?", id).Updates(item).Error
|
||||
}
|
||||
@@ -17,8 +17,8 @@ func HandleTerm(c *websocket.Conn) {
|
||||
hostRepo := hosts.NewRepository(&hosts.Hosts{User: user})
|
||||
data, err := hostRepo.Get(hostId)
|
||||
|
||||
if data == nil {
|
||||
log.Printf("Cannot find host! Error: %s\n", err.Error())
|
||||
if data == nil || !data.HasAccess(&user.User) {
|
||||
log.Printf("Cannot find host! %v\n", err)
|
||||
c.WriteMessage(websocket.TextMessage, []byte("Host not found"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@ func Init() {
|
||||
|
||||
// Migrate the schema
|
||||
db.AutoMigrate(Models...)
|
||||
InitModels(db)
|
||||
runSeeders(db)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"rul.sh/vaulterm/models"
|
||||
)
|
||||
|
||||
@@ -15,9 +12,3 @@ var Models = []interface{}{
|
||||
&models.Team{},
|
||||
&models.TeamMembers{},
|
||||
}
|
||||
|
||||
func InitModels(db *gorm.DB) {
|
||||
if err := db.SetupJoinTable(&models.Team{}, "Members", &models.TeamMembers{}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,9 +66,9 @@ func seedUsers(tx *gorm.DB) error {
|
||||
}
|
||||
|
||||
teamMembers := []models.TeamMembers{
|
||||
{TeamID: teams[0].ID, UserID: userList[0].ID, Role: "owner"},
|
||||
{TeamID: teams[0].ID, UserID: userList[1].ID, Role: "admin"},
|
||||
{TeamID: teams[0].ID, UserID: userList[2].ID, Role: "user"},
|
||||
{TeamID: teams[0].ID, UserID: userList[0].ID, Role: models.TeamRoleOwner},
|
||||
{TeamID: teams[0].ID, UserID: userList[1].ID, Role: models.TeamRoleAdmin},
|
||||
{TeamID: teams[0].ID, UserID: userList[2].ID, Role: models.TeamRoleMember},
|
||||
}
|
||||
|
||||
if res := tx.Create(&teamMembers); res.Error != nil {
|
||||
|
||||
+19
-10
@@ -4,7 +4,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
"rul.sh/vaulterm/db"
|
||||
"rul.sh/vaulterm/models"
|
||||
)
|
||||
@@ -23,25 +22,35 @@ func Auth(c *fiber.Ctx) error {
|
||||
|
||||
session, _ := GetUserSession(sessionId)
|
||||
|
||||
if session != nil && session.User.ID != "" {
|
||||
c.Locals("user", &session.User)
|
||||
if session != nil && session.ID != "" {
|
||||
c.Locals("user", session)
|
||||
c.Locals("sessionId", sessionId)
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
func GetUserSession(sessionId string) (*models.UserSession, error) {
|
||||
var session models.UserSession
|
||||
type AuthUser struct {
|
||||
models.User
|
||||
SessionID string `json:"sessionId" gorm:"column:session_id"`
|
||||
}
|
||||
|
||||
func GetUserSession(sessionId string) (*AuthUser, error) {
|
||||
var session AuthUser
|
||||
|
||||
res := db.Get().
|
||||
Joins("User").
|
||||
Preload("User.Teams", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Select("id", "name", "icon")
|
||||
}).
|
||||
Model(&models.User{}).
|
||||
Joins("JOIN user_sessions ON user_sessions.user_id = users.id").
|
||||
Preload("Teams.Team").
|
||||
Select("users.*, user_sessions.id AS session_id").
|
||||
Where("user_sessions.id = ?", sessionId).
|
||||
First(&session)
|
||||
|
||||
return &session, res.Error
|
||||
if res.Error != nil || session.User.ID == "" {
|
||||
return nil, res.Error
|
||||
}
|
||||
|
||||
return &session, nil
|
||||
}
|
||||
|
||||
func Protected() func(c *fiber.Ctx) error {
|
||||
|
||||
+21
-13
@@ -1,6 +1,8 @@
|
||||
package models
|
||||
|
||||
import "gorm.io/datatypes"
|
||||
import (
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
const (
|
||||
HostTypeSSH = "ssh"
|
||||
@@ -14,8 +16,10 @@ const (
|
||||
type Host struct {
|
||||
Model
|
||||
|
||||
OwnerID string `json:"userId" gorm:"index:hosts_owner_id_idx;type:varchar(26)"`
|
||||
Owner User `json:"user" gorm:"foreignKey:OwnerID"`
|
||||
OwnerID *string `json:"userId" gorm:"type:varchar(26)"`
|
||||
Owner *User `json:"user" gorm:"foreignKey:OwnerID"`
|
||||
TeamID *string `json:"teamId" gorm:"type:varchar(26)"`
|
||||
Team *Team `json:"team" gorm:"foreignKey:TeamID"`
|
||||
|
||||
Type string `json:"type" gorm:"not null;index:hosts_type_idx;type:varchar(16)"`
|
||||
Label string `json:"label"`
|
||||
@@ -24,11 +28,11 @@ type Host struct {
|
||||
OS string `json:"os" gorm:"type:varchar(32)"`
|
||||
Metadata datatypes.JSONMap `json:"metadata"`
|
||||
|
||||
ParentID *string `json:"parentId" gorm:"index:hosts_parent_id_idx;type:varchar(26)"`
|
||||
ParentID *string `json:"parentId" gorm:"type:varchar(26)"`
|
||||
Parent *Host `json:"parent" gorm:"foreignKey:ParentID"`
|
||||
KeyID *string `json:"keyId" gorm:"index:hosts_key_id_idx"`
|
||||
KeyID *string `json:"keyId" gorm:"type:varchar(26)"`
|
||||
Key Keychain `json:"key" gorm:"foreignKey:KeyID"`
|
||||
AltKeyID *string `json:"altKeyId" gorm:"index:hosts_altkey_id_idx"`
|
||||
AltKeyID *string `json:"altKeyId" gorm:"type:varchar(26)"`
|
||||
AltKey Keychain `json:"altKey" gorm:"foreignKey:AltKeyID"`
|
||||
|
||||
Timestamps
|
||||
@@ -58,13 +62,17 @@ func (h *Host) DecryptKeys() (*HostDecrypted, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
type HostHasAccessOptions struct {
|
||||
UserID string
|
||||
}
|
||||
|
||||
func (h *Host) HasAccess(o HostHasAccessOptions) bool {
|
||||
if o.UserID == h.OwnerID {
|
||||
func (h *Host) HasAccess(user *User) bool {
|
||||
if user.IsAdmin() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return *h.OwnerID == user.ID || user.IsInTeam(h.TeamID)
|
||||
}
|
||||
|
||||
func (h *Host) CanWrite(user *User) bool {
|
||||
if user.IsAdmin() {
|
||||
return true
|
||||
}
|
||||
teamRole := user.GetTeamRole(h.TeamID)
|
||||
return *h.OwnerID == user.ID || teamRole == TeamRoleOwner || teamRole == TeamRoleAdmin
|
||||
}
|
||||
|
||||
@@ -16,8 +16,10 @@ const (
|
||||
type Keychain struct {
|
||||
Model
|
||||
|
||||
OwnerID string `json:"userId" gorm:"index:hosts_owner_id_idx;type:varchar(26)"`
|
||||
Owner User `json:"user" gorm:"foreignKey:OwnerID"`
|
||||
OwnerID *string `json:"userId" gorm:"type:varchar(26)"`
|
||||
Owner *User `json:"user" gorm:"foreignKey:OwnerID"`
|
||||
TeamID *string `json:"teamId" gorm:"type:varchar(26)"`
|
||||
Team *Team `json:"team" gorm:"foreignKey:TeamID"`
|
||||
|
||||
Label string `json:"label"`
|
||||
Type string `json:"type" gorm:"not null;index:keychains_type_idx;type:varchar(12)"`
|
||||
@@ -55,3 +57,18 @@ func (k *Keychain) DecryptData(data interface{}) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *Keychain) HasAccess(user *User) bool {
|
||||
if user.IsAdmin() {
|
||||
return true
|
||||
}
|
||||
return *k.OwnerID == user.ID || user.IsInTeam(k.TeamID)
|
||||
}
|
||||
|
||||
func (k *Keychain) CanWrite(user *User) bool {
|
||||
if user.IsAdmin() {
|
||||
return true
|
||||
}
|
||||
teamRole := user.GetTeamRole(k.TeamID)
|
||||
return *k.OwnerID == user.ID || teamRole == TeamRoleOwner || teamRole == TeamRoleAdmin
|
||||
}
|
||||
|
||||
@@ -2,12 +2,18 @@ package models
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
TeamRoleOwner = "owner"
|
||||
TeamRoleAdmin = "admin"
|
||||
TeamRoleMember = "member"
|
||||
)
|
||||
|
||||
type Team struct {
|
||||
Model
|
||||
|
||||
Name string `json:"name" gorm:"type:varchar(32)"`
|
||||
Icon string `json:"icon" gorm:"type:varchar(2)"`
|
||||
Members []*User `json:"members" gorm:"many2many:team_members"`
|
||||
Name string `json:"name" gorm:"type:varchar(32)"`
|
||||
Icon string `json:"icon" gorm:"type:varchar(2)"`
|
||||
Members []*TeamMembers `json:"members" gorm:"foreignKey:TeamID"`
|
||||
|
||||
Timestamps
|
||||
SoftDeletes
|
||||
|
||||
+33
-1
@@ -1,5 +1,7 @@
|
||||
package models
|
||||
|
||||
import "slices"
|
||||
|
||||
const (
|
||||
UserRoleUser = "user"
|
||||
UserRoleAdmin = "admin"
|
||||
@@ -14,7 +16,7 @@ type User struct {
|
||||
Email string `json:"email" gorm:"unique"`
|
||||
Role string `json:"role" gorm:"default:user;not null;index:users_role_idx;type:varchar(8)"`
|
||||
|
||||
Teams []*Team `json:"teams" gorm:"many2many:team_members"`
|
||||
Teams []*TeamMembers `json:"teams" gorm:"foreignKey:UserID"`
|
||||
|
||||
Timestamps
|
||||
SoftDeletes
|
||||
@@ -28,3 +30,33 @@ type UserSession struct {
|
||||
Timestamps
|
||||
SoftDeletes
|
||||
}
|
||||
|
||||
func (u *User) IsAdmin() bool {
|
||||
return u.Role == UserRoleAdmin
|
||||
}
|
||||
|
||||
func (u *User) GetTeamRole(teamId *string) string {
|
||||
if u.IsAdmin() {
|
||||
return TeamRoleAdmin
|
||||
}
|
||||
if teamId == nil {
|
||||
return ""
|
||||
}
|
||||
idx := slices.IndexFunc(u.Teams, func(tm *TeamMembers) bool {
|
||||
return tm.TeamID == *teamId
|
||||
})
|
||||
if idx == -1 {
|
||||
return ""
|
||||
}
|
||||
return u.Teams[idx].Role
|
||||
}
|
||||
|
||||
func (u *User) IsInTeam(teamId *string) bool {
|
||||
role := u.GetTeamRole(teamId)
|
||||
return role != ""
|
||||
}
|
||||
|
||||
func (u *User) TeamCanWrite(teamId *string) bool {
|
||||
role := u.GetTeamRole(teamId)
|
||||
return role == TeamRoleAdmin || role == TeamRoleOwner
|
||||
}
|
||||
|
||||
+6
-22
@@ -3,33 +3,17 @@ package utils
|
||||
import (
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"rul.sh/vaulterm/models"
|
||||
"rul.sh/vaulterm/middleware"
|
||||
)
|
||||
|
||||
type UserContext struct {
|
||||
*models.User
|
||||
IsAdmin bool `json:"isAdmin"`
|
||||
}
|
||||
|
||||
func getUserData(user *models.User) *UserContext {
|
||||
isAdmin := false
|
||||
|
||||
if user.Role == models.UserRoleAdmin {
|
||||
isAdmin = true
|
||||
}
|
||||
|
||||
return &UserContext{
|
||||
User: user,
|
||||
IsAdmin: isAdmin,
|
||||
}
|
||||
}
|
||||
type UserContext = middleware.AuthUser
|
||||
|
||||
func GetUser(c *fiber.Ctx) *UserContext {
|
||||
user := c.Locals("user").(*models.User)
|
||||
return getUserData(user)
|
||||
user, _ := c.Locals("user").(*UserContext)
|
||||
return user
|
||||
}
|
||||
|
||||
func GetUserWs(c *websocket.Conn) *UserContext {
|
||||
user := c.Locals("user").(*models.User)
|
||||
return getUserData(user)
|
||||
user, _ := c.Locals("user").(*UserContext)
|
||||
return user
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user