mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:37:32 +00:00
refactor: remove unused code (#2614)
* fix(auth): switch project role requests to query pkg * refactor: delete unused project role code
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/caos/logging"
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/caos/zitadel/internal/project/model"
|
||||
)
|
||||
|
||||
type ProjectRole struct {
|
||||
es_models.ObjectRoot
|
||||
Key string `json:"key,omitempty"`
|
||||
DisplayName string `json:"displayName,omitempty"`
|
||||
Group string `json:"group,omitempty"`
|
||||
}
|
||||
|
||||
func GetProjectRole(roles []*ProjectRole, key string) (int, *ProjectRole) {
|
||||
for i, r := range roles {
|
||||
if r.Key == key {
|
||||
return i, r
|
||||
}
|
||||
}
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
func ProjectRolesToModel(roles []*ProjectRole) []*model.ProjectRole {
|
||||
convertedRoles := make([]*model.ProjectRole, len(roles))
|
||||
for i, r := range roles {
|
||||
convertedRoles[i] = ProjectRoleToModel(r)
|
||||
}
|
||||
return convertedRoles
|
||||
}
|
||||
|
||||
func ProjectRolesFromModel(roles []*model.ProjectRole) []*ProjectRole {
|
||||
convertedRoles := make([]*ProjectRole, len(roles))
|
||||
for i, r := range roles {
|
||||
convertedRoles[i] = ProjectRoleFromModel(r)
|
||||
}
|
||||
return convertedRoles
|
||||
}
|
||||
|
||||
func ProjectRoleFromModel(role *model.ProjectRole) *ProjectRole {
|
||||
return &ProjectRole{
|
||||
ObjectRoot: role.ObjectRoot,
|
||||
Key: role.Key,
|
||||
DisplayName: role.DisplayName,
|
||||
Group: role.Group,
|
||||
}
|
||||
}
|
||||
|
||||
func ProjectRoleToModel(role *ProjectRole) *model.ProjectRole {
|
||||
return &model.ProjectRole{
|
||||
ObjectRoot: role.ObjectRoot,
|
||||
Key: role.Key,
|
||||
DisplayName: role.DisplayName,
|
||||
Group: role.Group,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ProjectRole) setData(event *es_models.Event) error {
|
||||
r.ObjectRoot.AppendEvent(event)
|
||||
if err := json.Unmarshal(event.Data, r); err != nil {
|
||||
logging.Log("EVEN-d9euw").WithError(err).Error("could not unmarshal event data")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -1,82 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/caos/logging"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/caos/zitadel/internal/project/model"
|
||||
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
||||
)
|
||||
|
||||
const (
|
||||
ProjectRoleKeyKey = "role_key"
|
||||
ProjectRoleKeyOrgID = "org_id"
|
||||
ProjectRoleKeyProjectID = "project_id"
|
||||
ProjectRoleKeyResourceOwner = "resource_owner"
|
||||
)
|
||||
|
||||
type ProjectRoleView struct {
|
||||
OrgID string `json:"-" gorm:"column:org_id;primary_key"`
|
||||
ProjectID string `json:"projectId,omitempty" gorm:"column:project_id;primary_key"`
|
||||
Key string `json:"key" gorm:"column:role_key;primary_key"`
|
||||
DisplayName string `json:"displayName" gorm:"column:display_name"`
|
||||
Group string `json:"group" gorm:"column:group_name"`
|
||||
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
||||
|
||||
ResourceOwner string `json:"-" gorm:"resource_owner"`
|
||||
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
||||
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
||||
}
|
||||
|
||||
func ProjectRoleToModel(role *ProjectRoleView) *model.ProjectRoleView {
|
||||
return &model.ProjectRoleView{
|
||||
ResourceOwner: role.ResourceOwner,
|
||||
OrgID: role.OrgID,
|
||||
ProjectID: role.ProjectID,
|
||||
Key: role.Key,
|
||||
DisplayName: role.DisplayName,
|
||||
Group: role.Group,
|
||||
Sequence: role.Sequence,
|
||||
CreationDate: role.CreationDate,
|
||||
ChangeDate: role.ChangeDate,
|
||||
}
|
||||
}
|
||||
|
||||
func ProjectRolesToModel(roles []*ProjectRoleView) []*model.ProjectRoleView {
|
||||
result := make([]*model.ProjectRoleView, len(roles))
|
||||
for i, r := range roles {
|
||||
result[i] = ProjectRoleToModel(r)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (r *ProjectRoleView) AppendEvent(event *models.Event) (err error) {
|
||||
r.Sequence = event.Sequence
|
||||
switch event.Type {
|
||||
case es_model.ProjectRoleAdded:
|
||||
r.setRootData(event)
|
||||
r.CreationDate = event.CreationDate
|
||||
err = r.SetData(event)
|
||||
case es_model.ProjectRoleChanged:
|
||||
r.ChangeDate = event.CreationDate
|
||||
err = r.SetData(event)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *ProjectRoleView) setRootData(event *models.Event) {
|
||||
r.ProjectID = event.AggregateID
|
||||
r.OrgID = event.ResourceOwner
|
||||
r.ResourceOwner = event.ResourceOwner
|
||||
}
|
||||
|
||||
func (r *ProjectRoleView) SetData(event *models.Event) error {
|
||||
if err := json.Unmarshal(event.Data, r); err != nil {
|
||||
logging.Log("EVEN-slo9s").WithError(err).Error("could not unmarshal event data")
|
||||
return caos_errs.ThrowInternal(err, "MODEL-6z52s", "Could not unmarshal data")
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -1,65 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
proj_model "github.com/caos/zitadel/internal/project/model"
|
||||
"github.com/caos/zitadel/internal/view/repository"
|
||||
)
|
||||
|
||||
type ProjectRoleSearchRequest proj_model.ProjectRoleSearchRequest
|
||||
type ProjectRoleSearchQuery proj_model.ProjectRoleSearchQuery
|
||||
type ProjectRoleSearchKey proj_model.ProjectRoleSearchKey
|
||||
|
||||
func (req ProjectRoleSearchRequest) GetLimit() uint64 {
|
||||
return req.Limit
|
||||
}
|
||||
|
||||
func (req ProjectRoleSearchRequest) GetOffset() uint64 {
|
||||
return req.Offset
|
||||
}
|
||||
|
||||
func (req ProjectRoleSearchRequest) GetSortingColumn() repository.ColumnKey {
|
||||
if req.SortingColumn == proj_model.ProjectRoleSearchKeyUnspecified {
|
||||
return nil
|
||||
}
|
||||
return ProjectRoleSearchKey(req.SortingColumn)
|
||||
}
|
||||
|
||||
func (req ProjectRoleSearchRequest) GetAsc() bool {
|
||||
return req.Asc
|
||||
}
|
||||
|
||||
func (req ProjectRoleSearchRequest) GetQueries() []repository.SearchQuery {
|
||||
result := make([]repository.SearchQuery, len(req.Queries))
|
||||
for i, q := range req.Queries {
|
||||
result[i] = ProjectRoleSearchQuery{Key: q.Key, Value: q.Value, Method: q.Method}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (req ProjectRoleSearchQuery) GetKey() repository.ColumnKey {
|
||||
return ProjectRoleSearchKey(req.Key)
|
||||
}
|
||||
|
||||
func (req ProjectRoleSearchQuery) GetMethod() domain.SearchMethod {
|
||||
return req.Method
|
||||
}
|
||||
|
||||
func (req ProjectRoleSearchQuery) GetValue() interface{} {
|
||||
return req.Value
|
||||
}
|
||||
|
||||
func (key ProjectRoleSearchKey) ToColumnName() string {
|
||||
switch proj_model.ProjectRoleSearchKey(key) {
|
||||
case proj_model.ProjectRoleSearchKeyKey:
|
||||
return ProjectRoleKeyKey
|
||||
case proj_model.ProjectRoleSearchKeyOrgID:
|
||||
return ProjectRoleKeyOrgID
|
||||
case proj_model.ProjectRoleSearchKeyProjectID:
|
||||
return ProjectRoleKeyProjectID
|
||||
case proj_model.ProjectRoleSearchKeyResourceOwner:
|
||||
return ProjectRoleKeyResourceOwner
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
@@ -1,66 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
||||
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
||||
)
|
||||
|
||||
func mockProjectRoleData(member *es_model.ProjectRole) []byte {
|
||||
data, _ := json.Marshal(member)
|
||||
return data
|
||||
}
|
||||
|
||||
func TestProjectRoleAppendEvent(t *testing.T) {
|
||||
type args struct {
|
||||
event *es_models.Event
|
||||
member *ProjectRoleView
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
result *ProjectRoleView
|
||||
}{
|
||||
{
|
||||
name: "append added member event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: es_model.ProjectRoleAdded, ResourceOwner: "OrgID", Data: mockProjectRoleData(&es_model.ProjectRole{Key: "Key", DisplayName: "DisplayName", Group: "Group"})},
|
||||
member: &ProjectRoleView{},
|
||||
},
|
||||
result: &ProjectRoleView{OrgID: "OrgID", ResourceOwner: "OrgID", ProjectID: "AggregateID", Key: "Key", DisplayName: "DisplayName", Group: "Group"},
|
||||
},
|
||||
{
|
||||
name: "append added member event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: es_model.ProjectRoleAdded, ResourceOwner: "OrgID", Data: mockProjectRoleData(&es_model.ProjectRole{Key: "Key", DisplayName: "DisplayNameChanged", Group: "GroupChanged"})},
|
||||
member: &ProjectRoleView{OrgID: "OrgID", ResourceOwner: "OrgID", ProjectID: "AggregateID", Key: "Key", DisplayName: "DisplayName", Group: "Group"},
|
||||
},
|
||||
result: &ProjectRoleView{OrgID: "OrgID", ResourceOwner: "OrgID", ProjectID: "AggregateID", Key: "Key", DisplayName: "DisplayNameChanged", Group: "GroupChanged"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.args.member.AppendEvent(tt.args.event)
|
||||
if tt.args.member.ProjectID != tt.result.ProjectID {
|
||||
t.Errorf("got wrong result projectID: expected: %v, actual: %v ", tt.result.ProjectID, tt.args.member.ProjectID)
|
||||
}
|
||||
if tt.args.member.OrgID != tt.result.OrgID {
|
||||
t.Errorf("got wrong result orgID: expected: %v, actual: %v ", tt.result.OrgID, tt.args.member.OrgID)
|
||||
}
|
||||
if tt.args.member.ResourceOwner != tt.result.ResourceOwner {
|
||||
t.Errorf("got wrong result ResourceOwner: expected: %v, actual: %v ", tt.result.ResourceOwner, tt.args.member.ResourceOwner)
|
||||
}
|
||||
if tt.args.member.Key != tt.result.Key {
|
||||
t.Errorf("got wrong result Key: expected: %v, actual: %v ", tt.result.Key, tt.args.member.Key)
|
||||
}
|
||||
if tt.args.member.DisplayName != tt.result.DisplayName {
|
||||
t.Errorf("got wrong result DisplayName: expected: %v, actual: %v ", tt.result.DisplayName, tt.args.member.DisplayName)
|
||||
}
|
||||
if tt.args.member.Group != tt.result.Group {
|
||||
t.Errorf("got wrong result Group: expected: %v, actual: %v ", tt.result.Group, tt.args.member.Group)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -1,96 +0,0 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
proj_model "github.com/caos/zitadel/internal/project/model"
|
||||
"github.com/caos/zitadel/internal/project/repository/view/model"
|
||||
"github.com/caos/zitadel/internal/view/repository"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
func ProjectRoleByIDs(db *gorm.DB, table, projectID, orgID, key string) (*model.ProjectRoleView, error) {
|
||||
role := new(model.ProjectRoleView)
|
||||
|
||||
projectIDQuery := model.ProjectRoleSearchQuery{Key: proj_model.ProjectRoleSearchKeyProjectID, Value: projectID, Method: domain.SearchMethodEquals}
|
||||
grantIDQuery := model.ProjectRoleSearchQuery{Key: proj_model.ProjectRoleSearchKeyOrgID, Value: orgID, Method: domain.SearchMethodEquals}
|
||||
keyQuery := model.ProjectRoleSearchQuery{Key: proj_model.ProjectRoleSearchKeyKey, Value: key, Method: domain.SearchMethodEquals}
|
||||
query := repository.PrepareGetByQuery(table, projectIDQuery, grantIDQuery, keyQuery)
|
||||
err := query(db, role)
|
||||
if caos_errs.IsNotFound(err) {
|
||||
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Wtg72", "Errors.Project.Role.NotExisting")
|
||||
}
|
||||
return role, err
|
||||
}
|
||||
|
||||
func ProjectRolesByProjectID(db *gorm.DB, table, projectID string) ([]*model.ProjectRoleView, error) {
|
||||
roles := make([]*model.ProjectRoleView, 0)
|
||||
queries := []*proj_model.ProjectRoleSearchQuery{
|
||||
{Key: proj_model.ProjectRoleSearchKeyProjectID, Value: projectID, Method: domain.SearchMethodEquals},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.ProjectRoleSearchRequest{Queries: queries})
|
||||
_, err := query(db, &roles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
|
||||
func ResourceOwnerProjectRolesByKey(db *gorm.DB, table, projectID, resourceOwner, key string) ([]*model.ProjectRoleView, error) {
|
||||
roles := make([]*model.ProjectRoleView, 0)
|
||||
queries := []*proj_model.ProjectRoleSearchQuery{
|
||||
{Key: proj_model.ProjectRoleSearchKeyProjectID, Value: projectID, Method: domain.SearchMethodEquals},
|
||||
{Key: proj_model.ProjectRoleSearchKeyResourceOwner, Value: resourceOwner, Method: domain.SearchMethodEquals},
|
||||
{Key: proj_model.ProjectRoleSearchKeyKey, Value: key, Method: domain.SearchMethodEquals},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.ProjectRoleSearchRequest{Queries: queries})
|
||||
_, err := query(db, &roles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
|
||||
func ResourceOwnerProjectRoles(db *gorm.DB, table, projectID, resourceOwner string) ([]*model.ProjectRoleView, error) {
|
||||
roles := make([]*model.ProjectRoleView, 0)
|
||||
queries := []*proj_model.ProjectRoleSearchQuery{
|
||||
{Key: proj_model.ProjectRoleSearchKeyProjectID, Value: projectID, Method: domain.SearchMethodEquals},
|
||||
{Key: proj_model.ProjectRoleSearchKeyResourceOwner, Value: resourceOwner, Method: domain.SearchMethodEquals},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.ProjectRoleSearchRequest{Queries: queries})
|
||||
_, err := query(db, &roles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
|
||||
func SearchProjectRoles(db *gorm.DB, table string, req *proj_model.ProjectRoleSearchRequest) ([]*model.ProjectRoleView, uint64, error) {
|
||||
roles := make([]*model.ProjectRoleView, 0)
|
||||
query := repository.PrepareSearchQuery(table, model.ProjectRoleSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
||||
count, err := query(db, &roles)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return roles, count, nil
|
||||
}
|
||||
|
||||
func PutProjectRole(db *gorm.DB, table string, role *model.ProjectRoleView) error {
|
||||
save := repository.PrepareSave(table)
|
||||
return save(db, role)
|
||||
}
|
||||
|
||||
func DeleteProjectRole(db *gorm.DB, table, projectID, orgID, key string) error {
|
||||
role, err := ProjectRoleByIDs(db, table, projectID, orgID, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
delete := repository.PrepareDeleteByObject(table, role)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func DeleteProjectRolesByProjectID(db *gorm.DB, table, projectID string) error {
|
||||
delete := repository.PrepareDeleteByKey(table, model.ProjectRoleSearchKey(proj_model.ProjectRoleSearchKeyProjectID), projectID)
|
||||
return delete(db)
|
||||
|
||||
}
|
Reference in New Issue
Block a user