From d4fbc33f3db2a43ae1ceff15e050652d616575ac Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Mon, 1 Aug 2022 17:10:58 +0530 Subject: [PATCH] Fix Action logs placeholders for resources Mainly, It was broken for creation of most resources due to misplace for action-log prop. --- automation/service/workflow.go | 4 +++- compose/service/chart.go | 4 +++- compose/service/module.go | 4 ++-- compose/service/namespace.go | 4 +++- compose/service/page.go | 4 +++- system/service/apigw_route.go | 4 +++- system/service/application.go | 4 +++- system/service/attachment.go | 8 ++++---- system/service/auth.go | 6 ++++-- system/service/auth_client.go | 4 +++- system/service/auth_credentials.go | 12 ++++++++---- system/service/auth_credentials_test.go | 6 +++--- system/service/report.go | 4 +++- system/service/role.go | 16 +++++++++------- 14 files changed, 54 insertions(+), 30 deletions(-) diff --git a/automation/service/workflow.go b/automation/service/workflow.go index dca591dc6..510046a49 100644 --- a/automation/service/workflow.go +++ b/automation/service/workflow.go @@ -193,7 +193,7 @@ func (svc *workflow) LookupByID(ctx context.Context, workflowID uint64) (wf *typ // It updates service's cache func (svc *workflow) Create(ctx context.Context, new *types.Workflow) (wf *types.Workflow, err error) { var ( - wap = &workflowActionProps{new: new} + wap = &workflowActionProps{workflow: new} cUser = intAuth.GetIdentityFromContext(ctx).Identity() g *wfexec.Graph runAs intAuth.Identifiable @@ -252,6 +252,8 @@ func (svc *workflow) Create(ctx context.Context, new *types.Workflow) (wf *types return } + wap.setNew(wf) + return }) diff --git a/compose/service/chart.go b/compose/service/chart.go index 87eda3aa6..c66b90f36 100644 --- a/compose/service/chart.go +++ b/compose/service/chart.go @@ -145,7 +145,7 @@ func (svc chart) Create(ctx context.Context, new *types.Chart) (*types.Chart, er var ( err error ns *types.Namespace - aProps = &chartActionProps{changed: new} + aProps = &chartActionProps{chart: new} ) err = store.Tx(ctx, svc.store, func(ctx context.Context, s store.Storer) (err error) { @@ -175,6 +175,8 @@ func (svc chart) Create(ctx context.Context, new *types.Chart) (*types.Chart, er // generate config element IDs new.Config.GenerateIDs(nextID) + aProps.setChanged(new) + if err = store.CreateComposeChart(ctx, s, new); err != nil { return err } diff --git a/compose/service/module.go b/compose/service/module.go index f7fba0f08..72f9781fd 100644 --- a/compose/service/module.go +++ b/compose/service/module.go @@ -314,7 +314,7 @@ func (svc module) procDal(m *types.Module) { func (svc module) Create(ctx context.Context, new *types.Module) (*types.Module, error) { var ( ns *types.Namespace - aProps = &moduleActionProps{changed: new} + aProps = &moduleActionProps{module: new} ) err := store.Tx(ctx, svc.store, func(ctx context.Context, s store.Storer) (err error) { @@ -382,7 +382,7 @@ func (svc module) Create(ctx context.Context, new *types.Module) (*types.Module, } } - aProps.setModule(new) + aProps.setChanged(new) if err = store.CreateComposeModule(ctx, s, new); err != nil { return err diff --git a/compose/service/namespace.go b/compose/service/namespace.go index 73ee36895..a8df3ecb5 100644 --- a/compose/service/namespace.go +++ b/compose/service/namespace.go @@ -218,7 +218,7 @@ func (svc namespace) FindByAny(ctx context.Context, identifier interface{}) (r * // Create adds namespace and presets access rules for role everyone func (svc namespace) Create(ctx context.Context, new *types.Namespace) (*types.Namespace, error) { var ( - aProps = &namespaceActionProps{changed: new} + aProps = &namespaceActionProps{namespace: new} ) err := store.Tx(ctx, svc.store, func(ctx context.Context, s store.Storer) (err error) { @@ -243,6 +243,8 @@ func (svc namespace) Create(ctx context.Context, new *types.Namespace) (*types.N new.UpdatedAt = nil new.DeletedAt = nil + aProps.setChanged(new) + if err = store.CreateComposeNamespace(ctx, svc.store, new); err != nil { return err } diff --git a/compose/service/page.go b/compose/service/page.go index c2a05475f..e7cf7ba16 100644 --- a/compose/service/page.go +++ b/compose/service/page.go @@ -274,7 +274,7 @@ func (svc page) Reorder(ctx context.Context, namespaceID, parentID uint64, pageI func (svc page) Create(ctx context.Context, new *types.Page) (*types.Page, error) { var ( ns *types.Namespace - aProps = &pageActionProps{changed: new} + aProps = &pageActionProps{page: new} ) new.ID = 0 @@ -312,6 +312,8 @@ func (svc page) Create(ctx context.Context, new *types.Page) (*types.Page, error new.Blocks[i].BlockID = uint64(i) + 1 } + aProps.setChanged(new) + if err = store.CreateComposePage(ctx, s, new); err != nil { return err } diff --git a/system/service/apigw_route.go b/system/service/apigw_route.go index 80b1f4914..7b90cb3dc 100644 --- a/system/service/apigw_route.go +++ b/system/service/apigw_route.go @@ -62,7 +62,7 @@ func (svc *apigwRoute) FindByID(ctx context.Context, ID uint64) (q *types.ApigwR func (svc *apigwRoute) Create(ctx context.Context, new *types.ApigwRoute) (q *types.ApigwRoute, err error) { var ( - qProps = &apigwRouteActionProps{new: new} + qProps = &apigwRouteActionProps{route: new} ) err = func() (err error) { @@ -77,6 +77,8 @@ func (svc *apigwRoute) Create(ctx context.Context, new *types.ApigwRoute) (q *ty // todo new.Group = 0 + qProps.setNew(new) + if err = store.CreateApigwRoute(ctx, svc.store, new); err != nil { return err } diff --git a/system/service/application.go b/system/service/application.go index 311e01301..238321723 100644 --- a/system/service/application.go +++ b/system/service/application.go @@ -148,7 +148,7 @@ func (svc *application) Search(ctx context.Context, af types.ApplicationFilter) func (svc *application) Create(ctx context.Context, new *types.Application) (app *types.Application, err error) { var ( - aaProps = &applicationActionProps{new: new} + aaProps = &applicationActionProps{application: new} ) err = func() (err error) { @@ -168,6 +168,8 @@ func (svc *application) Create(ctx context.Context, new *types.Application) (app new.Unify = &types.ApplicationUnify{} } + aaProps.setNew(new) + if err = store.CreateApplication(ctx, svc.store, new); err != nil { return } diff --git a/system/service/attachment.go b/system/service/attachment.go index c5d57f1ad..dc5d847ff 100644 --- a/system/service/attachment.go +++ b/system/service/attachment.go @@ -172,10 +172,6 @@ func (svc attachment) CreateApplicationAttachment(ctx context.Context, name stri ) err = func() (err error) { - if !svc.ac.CanCreateApplication(ctx) { - return AttachmentErrNotAllowedToCreate() - } - att = &types.Attachment{ OwnerID: currentUserID, Name: strings.TrimSpace(name), @@ -184,6 +180,10 @@ func (svc attachment) CreateApplicationAttachment(ctx context.Context, name stri aaProps.setAttachment(att) + if !svc.ac.CanCreateApplication(ctx) { + return AttachmentErrNotAllowedToCreate() + } + if labels != nil { att.Meta.Labels = labels } diff --git a/system/service/auth.go b/system/service/auth.go index d1ab8ad59..9a7d83abb 100644 --- a/system/service/auth.go +++ b/system/service/auth.go @@ -329,7 +329,8 @@ func (svc *auth) InternalSignUp(ctx context.Context, input *types.User, password } // does password match any of the valid credentials? - if !isValidPassword(cc, password) { + c = findValidPassword(cc, password) + if c == nil { return AuthErrInvalidCredentials(aam) } @@ -480,7 +481,8 @@ func (svc *auth) InternalLogin(ctx context.Context, email string, password strin } // find 1st valid credentials that match the hashed password - if !isValidPassword(cc, password) { + c = findValidPassword(cc, password) + if c == nil { return AuthErrInvalidCredentials(aam) } diff --git a/system/service/auth_client.go b/system/service/auth_client.go index c0282917c..70668acfd 100644 --- a/system/service/auth_client.go +++ b/system/service/auth_client.go @@ -183,7 +183,7 @@ func (svc *authClient) Search(ctx context.Context, af types.AuthClientFilter) (a func (svc *authClient) Create(ctx context.Context, new *types.AuthClient) (res *types.AuthClient, err error) { var ( - aaProps = &authClientActionProps{new: new} + aaProps = &authClientActionProps{authClient: new} ) err = func() (err error) { @@ -215,6 +215,8 @@ func (svc *authClient) Create(ctx context.Context, new *types.AuthClient) (res * } } + aaProps.setNew(new) + if err = store.CreateAuthClient(ctx, svc.store, new); err != nil { return } diff --git a/system/service/auth_credentials.go b/system/service/auth_credentials.go index 374f5a2a8..96cbc1c1c 100644 --- a/system/service/auth_credentials.go +++ b/system/service/auth_credentials.go @@ -500,7 +500,7 @@ func (svc *auth) ChangePassword(ctx context.Context, userID uint64, oldPassword, return err } - if !isValidPassword(cc, oldPassword) { + if c := findValidPassword(cc, oldPassword); c == nil { return AuthErrPasswordResetFailedOldPasswordCheckFailed(aam) } @@ -1039,12 +1039,16 @@ func validateToken(token string) (ID uint64, credentials string) { return } -// returns true if (hashed version of a) password is found in the +// returns matching credential if (hashed version of a) password is found in the // list of (valid) credentials // // should be used as a parameter for credentialsFilter fn -func isValidPassword(cc []*types.Credential, password string) bool { - return len(credentialsFilter(cc, 1, skipInvalid, compareHashedCredentials(password))) > 0 +func findValidPassword(cc []*types.Credential, password string) (c *types.Credential) { + cList := credentialsFilter(cc, 1, skipInvalid, compareHashedCredentials(password)) + if len(cList) > 0 { + c = cList[0] + } + return } // returns true if (hashed version of a) password is found in the diff --git a/system/service/auth_credentials_test.go b/system/service/auth_credentials_test.go index e64de1b6d..eda6e6be1 100644 --- a/system/service/auth_credentials_test.go +++ b/system/service/auth_credentials_test.go @@ -62,13 +62,13 @@ func Test_isValidPassword(t *testing.T) { t.Run(c.name, func(t *testing.T) { var ( req = require.New(t) - rsp = isValidPassword(c.cc, c.password) + rsp = findValidPassword(c.cc, c.password) ) if c.rval { - req.True(rsp) + req.NotNil(rsp) } else { - req.False(rsp) + req.Nil(rsp) } }) } diff --git a/system/service/report.go b/system/service/report.go index 6f5214912..7ae7bc9b6 100644 --- a/system/service/report.go +++ b/system/service/report.go @@ -131,7 +131,7 @@ func (svc *report) Search(ctx context.Context, rf types.ReportFilter) (rr types. func (svc *report) Create(ctx context.Context, new *types.Report) (report *types.Report, err error) { var ( - aaProps = &reportActionProps{new: new} + aaProps = &reportActionProps{report: new} ) err = func() (err error) { @@ -153,6 +153,8 @@ func (svc *report) Create(ctx context.Context, new *types.Report) (report *types new = svc.setIDs(new) + aaProps.setNew(new) + if err = store.CreateReport(ctx, svc.store, new); err != nil { return } diff --git a/system/service/role.go b/system/service/role.go index 44053c15a..545f54967 100644 --- a/system/service/role.go +++ b/system/service/role.go @@ -299,7 +299,7 @@ func (svc role) proc(ctx context.Context, r *types.Role, err error) (*types.Role func (svc role) Create(ctx context.Context, new *types.Role) (r *types.Role, err error) { var ( - raProps = &roleActionProps{new: new} + raProps = &roleActionProps{role: new} ) err = func() (err error) { @@ -328,6 +328,8 @@ func (svc role) Create(ctx context.Context, new *types.Role) (r *types.Role, err new.ID = nextID() new.CreatedAt = *now() + raProps.setNew(new) + if err = store.CreateRole(ctx, svc.store, new); err != nil { return } @@ -352,6 +354,12 @@ func (svc role) Update(ctx context.Context, upd *types.Role) (r *types.Role, err ) err = func() (err error) { + if r, err = loadRole(ctx, svc.store, upd.ID); err != nil { + return + } + + raProps.setRole(r) + if !handle.IsValid(upd.Handle) { return RoleErrInvalidHandle() } @@ -360,12 +368,6 @@ func (svc role) Update(ctx context.Context, upd *types.Role) (r *types.Role, err return RoleErrNotAllowedToUpdate() } - if r, err = loadRole(ctx, svc.store, upd.ID); err != nil { - return - } - - raProps.setRole(r) - if svc.IsSystem(r) { // prevent system role updates // we need this here because of the clumsy way