mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 11:15:12 +00:00
Multi tenant groups (#77)
* multitenant groups * switch users service to use new wrapper * fix tests * skip pkg dir * Check for auth
This commit is contained in:
@@ -5,8 +5,12 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/micro/v3/service/auth"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/groups/proto"
|
||||
gorm2 "github.com/micro/services/pkg/gorm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -41,10 +45,14 @@ func (g *Group) Serialize() *pb.Group {
|
||||
}
|
||||
|
||||
type Groups struct {
|
||||
DB *gorm.DB
|
||||
gorm2.Helper
|
||||
}
|
||||
|
||||
func (g *Groups) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.CreateResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
// validate the request
|
||||
if len(req.Name) == 0 {
|
||||
return ErrMissingName
|
||||
@@ -52,7 +60,13 @@ func (g *Groups) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.Crea
|
||||
|
||||
// create the group object
|
||||
group := &Group{ID: uuid.New().String(), Name: req.Name}
|
||||
if err := g.DB.Create(group).Error; err != nil {
|
||||
db, err := g.GetDBConn(ctx)
|
||||
if err != nil {
|
||||
logger.Errorf("Error connecting to DB: %v", err)
|
||||
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
|
||||
}
|
||||
|
||||
if err := db.Create(group).Error; err != nil {
|
||||
return ErrStore
|
||||
}
|
||||
|
||||
@@ -62,14 +76,23 @@ func (g *Groups) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.Crea
|
||||
}
|
||||
|
||||
func (g *Groups) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
// validate the request
|
||||
if len(req.Ids) == 0 {
|
||||
return ErrMissingIDs
|
||||
}
|
||||
|
||||
db, err := g.GetDBConn(ctx)
|
||||
if err != nil {
|
||||
logger.Errorf("Error connecting to DB: %v", err)
|
||||
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
|
||||
}
|
||||
// query the database
|
||||
var groups []Group
|
||||
if err := g.DB.Model(&Group{}).Preload("Memberships").Where("id IN (?)", req.Ids).Find(&groups).Error; err != nil {
|
||||
if err := db.Model(&Group{}).Preload("Memberships").Where("id IN (?)", req.Ids).Find(&groups).Error; err != nil {
|
||||
return ErrStore
|
||||
}
|
||||
|
||||
@@ -83,6 +106,10 @@ func (g *Groups) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResp
|
||||
}
|
||||
|
||||
func (g *Groups) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.UpdateResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
// validate the request
|
||||
if len(req.Id) == 0 {
|
||||
return ErrMissingID
|
||||
@@ -90,8 +117,13 @@ func (g *Groups) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.Upda
|
||||
if len(req.Name) == 0 {
|
||||
return ErrMissingName
|
||||
}
|
||||
db, err := g.GetDBConn(ctx)
|
||||
if err != nil {
|
||||
logger.Errorf("Error connecting to DB: %v", err)
|
||||
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
|
||||
}
|
||||
|
||||
return g.DB.Transaction(func(tx *gorm.DB) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
// find the group
|
||||
var group Group
|
||||
if err := tx.Where(&Group{ID: req.Id}).First(&group).Error; err == gorm.ErrRecordNotFound {
|
||||
@@ -113,13 +145,22 @@ func (g *Groups) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.Upda
|
||||
}
|
||||
|
||||
func (g *Groups) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
// validate the request
|
||||
if len(req.Id) == 0 {
|
||||
return ErrMissingID
|
||||
}
|
||||
|
||||
db, err := g.GetDBConn(ctx)
|
||||
if err != nil {
|
||||
logger.Errorf("Error connecting to DB: %v", err)
|
||||
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
|
||||
}
|
||||
// delete from the database
|
||||
if err := g.DB.Delete(&Group{ID: req.Id}).Error; err == gorm.ErrRecordNotFound {
|
||||
if err := db.Delete(&Group{ID: req.Id}).Error; err == gorm.ErrRecordNotFound {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return ErrStore
|
||||
@@ -129,10 +170,19 @@ func (g *Groups) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.Dele
|
||||
}
|
||||
|
||||
func (g *Groups) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
db, err := g.GetDBConn(ctx)
|
||||
if err != nil {
|
||||
logger.Errorf("Error connecting to DB: %v", err)
|
||||
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
|
||||
}
|
||||
if len(req.MemberId) > 0 {
|
||||
// only list groups the user is a member of
|
||||
var ms []Membership
|
||||
q := g.DB.Where(&Membership{MemberID: req.MemberId}).Preload("Group.Memberships")
|
||||
q := db.Where(&Membership{MemberID: req.MemberId}).Preload("Group.Memberships")
|
||||
if err := q.Find(&ms).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -145,7 +195,7 @@ func (g *Groups) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResp
|
||||
|
||||
// load all groups
|
||||
var groups []Group
|
||||
if err := g.DB.Model(&Group{}).Preload("Memberships").Find(&groups).Error; err != nil {
|
||||
if err := db.Model(&Group{}).Preload("Memberships").Find(&groups).Error; err != nil {
|
||||
return ErrStore
|
||||
}
|
||||
|
||||
@@ -159,6 +209,10 @@ func (g *Groups) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResp
|
||||
}
|
||||
|
||||
func (g *Groups) AddMember(ctx context.Context, req *pb.AddMemberRequest, rsp *pb.AddMemberResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
// validate the request
|
||||
if len(req.GroupId) == 0 {
|
||||
return ErrMissingGroupID
|
||||
@@ -166,8 +220,13 @@ func (g *Groups) AddMember(ctx context.Context, req *pb.AddMemberRequest, rsp *p
|
||||
if len(req.MemberId) == 0 {
|
||||
return ErrMissingMemberID
|
||||
}
|
||||
db, err := g.GetDBConn(ctx)
|
||||
if err != nil {
|
||||
logger.Errorf("Error connecting to DB: %v", err)
|
||||
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
|
||||
}
|
||||
|
||||
return g.DB.Transaction(func(tx *gorm.DB) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
// check the group exists
|
||||
var group Group
|
||||
if err := tx.Where(&Group{ID: req.GroupId}).First(&group).Error; err == gorm.ErrRecordNotFound {
|
||||
@@ -191,6 +250,10 @@ func (g *Groups) AddMember(ctx context.Context, req *pb.AddMemberRequest, rsp *p
|
||||
}
|
||||
|
||||
func (g *Groups) RemoveMember(ctx context.Context, req *pb.RemoveMemberRequest, rsp *pb.RemoveMemberResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
// validate the request
|
||||
if len(req.GroupId) == 0 {
|
||||
return ErrMissingGroupID
|
||||
@@ -199,9 +262,14 @@ func (g *Groups) RemoveMember(ctx context.Context, req *pb.RemoveMemberRequest,
|
||||
return ErrMissingMemberID
|
||||
}
|
||||
|
||||
db, err := g.GetDBConn(ctx)
|
||||
if err != nil {
|
||||
logger.Errorf("Error connecting to DB: %v", err)
|
||||
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
|
||||
}
|
||||
// delete the membership
|
||||
m := &Membership{MemberID: req.MemberId, GroupID: req.GroupId}
|
||||
if err := g.DB.Where(m).Delete(m).Error; err != nil {
|
||||
if err := db.Where(m).Delete(m).Error; err != nil {
|
||||
return ErrStore
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user