Skip to content

Commit

Permalink
feat: org and user avatar presigned url; remove additional field (#382)
Browse files Browse the repository at this point in the history
* feat: add avatar file for organizations, add presigned urls for all file queries

Signed-off-by: Sarah Funkhouser <[email protected]>

* fix bug

Signed-off-by: Sarah Funkhouser <[email protected]>

* generate config

Signed-off-by: Sarah Funkhouser <[email protected]>

* fix tests, run generate

Signed-off-by: Sarah Funkhouser <[email protected]>

* Fix comments

Signed-off-by: Sarah Funkhouser <[email protected]>

---------

Signed-off-by: Sarah Funkhouser <[email protected]>
  • Loading branch information
golanglemonade authored Jan 15, 2025
1 parent 647a80d commit 86423d2
Show file tree
Hide file tree
Showing 95 changed files with 5,133 additions and 2,562 deletions.
28 changes: 23 additions & 5 deletions cmd/cli/cmd/organization/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package org
import (
"context"

"github.com/99designs/gqlgen/graphql"
"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
"github.com/theopenlane/core/pkg/objects"
"github.com/theopenlane/core/pkg/openlaneclient"
)

Expand All @@ -26,17 +28,18 @@ func init() {
createCmd.Flags().StringP("description", "d", "", "description of the organization")
createCmd.Flags().StringP("parent-org-id", "p", "", "parent organization id, leave empty to create a root org")
createCmd.Flags().StringSlice("tags", []string{}, "tags associated with the organization")
createCmd.Flags().StringP("avatar-file", "a", "", "local of avatar file to upload")

// TODO: https://github.com/theopenlane/core/issues/734
// remove flag once the feature is implemented
createCmd.Flags().BoolP("dedicated-db", "D", false, "create a dedicated database for the organization")
}

// createValidation validates the required fields for the command
func createValidation() (input openlaneclient.CreateOrganizationInput, err error) {
func createValidation() (input openlaneclient.CreateOrganizationInput, avatarFile *graphql.Upload, err error) {
input.Name = cmd.Config.String("name")
if input.Name == "" {
return input, cmd.NewRequiredFieldMissingError("organization name")
return input, nil, cmd.NewRequiredFieldMissingError("organization name")
}

displayName := cmd.Config.String("display-name")
Expand Down Expand Up @@ -64,7 +67,22 @@ func createValidation() (input openlaneclient.CreateOrganizationInput, err error
input.Tags = tags
}

return input, nil
avatarFileLoc := cmd.Config.String("avatar-file")
if avatarFileLoc != "" {
file, err := objects.NewUploadFile(avatarFileLoc)
if err != nil {
return input, nil, err
}

avatarFile = &graphql.Upload{
File: file.File,
Filename: file.Filename,
Size: file.Size,
ContentType: file.ContentType,
}
}

return input, avatarFile, nil
}

// create an organization in the platform
Expand All @@ -78,10 +96,10 @@ func create(ctx context.Context) error {
defer cmd.StoreSessionCookies(client)
}

input, err := createValidation()
input, avatarFile, err := createValidation()
cobra.CheckErr(err)

o, err := client.CreateOrganization(ctx, input)
o, err := client.CreateOrganization(ctx, input, avatarFile)
cobra.CheckErr(err)

return consoleOutput(o)
Expand Down
27 changes: 22 additions & 5 deletions cmd/cli/cmd/organization/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package org
import (
"context"

"github.com/99designs/gqlgen/graphql"
"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
"github.com/theopenlane/core/pkg/objects"
"github.com/theopenlane/core/pkg/openlaneclient"
)

Expand All @@ -28,10 +30,10 @@ func init() {
}

// updateValidation validates the required fields for the command
func updateValidation() (id string, input openlaneclient.UpdateOrganizationInput, err error) {
func updateValidation() (id string, input openlaneclient.UpdateOrganizationInput, avatarFile *graphql.Upload, err error) {
id = cmd.Config.String("id")
if id == "" {
return id, input, cmd.NewRequiredFieldMissingError("organization id")
return id, input, nil, cmd.NewRequiredFieldMissingError("organization id")
}

name := cmd.Config.String("name")
Expand All @@ -49,7 +51,22 @@ func updateValidation() (id string, input openlaneclient.UpdateOrganizationInput
input.Description = &description
}

return id, input, nil
avatarFileLoc := cmd.Config.String("avatar-file")
if avatarFileLoc != "" {
file, err := objects.NewUploadFile(avatarFileLoc)
if err != nil {
return id, input, nil, err
}

avatarFile = &graphql.Upload{
File: file.File,
Filename: file.Filename,
Size: file.Size,
ContentType: file.ContentType,
}
}

return id, input, avatarFile, nil
}

// update an existing organization in the platform
Expand All @@ -63,10 +80,10 @@ func update(ctx context.Context) error {
defer cmd.StoreSessionCookies(client)
}

id, input, err := updateValidation()
id, input, avatarFile, err := updateValidation()
cobra.CheckErr(err)

o, err := client.UpdateOrganization(ctx, id, input)
o, err := client.UpdateOrganization(ctx, id, input, avatarFile)
cobra.CheckErr(err)

return consoleOutput(o)
Expand Down
1 change: 1 addition & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func serve(ctx context.Context) error {
ent.EntConfig(&so.Config.Settings.EntConfig),
ent.Emailer(&so.Config.Settings.Email),
ent.EntitlementManager(so.Config.Handler.Entitlements),
ent.ObjectManager(so.Config.ObjectManager),
)

// Setup DB connection
Expand Down
1 change: 1 addition & 0 deletions config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ CORE_OBJECTSTORAGE_REGION=""
CORE_OBJECTSTORAGE_SECRETKEY=""
CORE_OBJECTSTORAGE_CREDENTIALSJSON=""
CORE_OBJECTSTORAGE_DEFAULTBUCKET="file_uploads"
CORE_OBJECTSTORAGE_LOCALURL="http://localhost:17608/files/"
CORE_OBJECTSTORAGE_KEYS="[uploadFile]"
CORE_SUBSCRIPTION_ENABLED="false"
CORE_SUBSCRIPTION_PUBLICSTRIPEKEY=""
Expand Down
12 changes: 11 additions & 1 deletion config/config-dev.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,19 @@ entConfig:
useListUserService: true

objectStorage:
# when using local storage, we serve the files up on a static handler
# if switching to s3, be sure to use the below defaultBucket instead
defaultBucket: "file_uploads
# uncomment and update to enable s3, otherwise it will default to local storage
# accessKey: "REDACTED"
# secretKey: "REDACTED"
# region: "us-east-2"
# defaultBucket: "openlane"
# defaultBucket: "openlane-development"
# provider: "s3"

subscription:
enabled: false
privateStripeKey: "sk_test_REDCATED"
publicStripeKey: "pk_test_REDACTED"
trialSubscriptionPriceID: price_REDACTED
stripeBillingPortalSuccessURL: http://localhost:3001/organization-settings/billing
1 change: 1 addition & 0 deletions config/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ objectStorage:
enabled: true
keys:
- uploadFile
localURL: http://localhost:17608/files/
provider: ""
region: ""
secretKey: ""
Expand Down
1 change: 1 addition & 0 deletions config/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ data:
CORE_OBJECTSTORAGE_SECRETKEY: {{ .Values.core.objectStorage.secretKey }}
CORE_OBJECTSTORAGE_CREDENTIALSJSON: {{ .Values.core.objectStorage.credentialsJSON }}
CORE_OBJECTSTORAGE_DEFAULTBUCKET: {{ .Values.core.objectStorage.defaultBucket | default "file_uploads" }}
CORE_OBJECTSTORAGE_LOCALURL: {{ .Values.core.objectStorage.localURL | default "http://localhost:17608/files/" }}
CORE_OBJECTSTORAGE_KEYS: {{ .Values.core.objectStorage.keys | default "uploadFile" }}
CORE_SUBSCRIPTION_ENABLED: {{ .Values.core.subscription.enabled | default false }}
CORE_SUBSCRIPTION_PUBLICSTRIPEKEY: {{ .Values.core.subscription.publicStripeKey }}
Expand Down
19 changes: 19 additions & 0 deletions db/migrations-goose-postgres/20250114212723_avatar_files.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- +goose Up
-- modify "organization_history" table
ALTER TABLE "organization_history" ADD COLUMN "avatar_local_file_id" character varying NULL, ADD COLUMN "avatar_updated_at" timestamptz NULL;
-- modify "user_history" table
ALTER TABLE "user_history" DROP COLUMN "avatar_local_file";
-- modify "organizations" table
ALTER TABLE "organizations" ADD COLUMN "avatar_updated_at" timestamptz NULL, ADD COLUMN "avatar_local_file_id" character varying NULL, ADD CONSTRAINT "organizations_files_avatar_file" FOREIGN KEY ("avatar_local_file_id") REFERENCES "files" ("id") ON UPDATE NO ACTION ON DELETE SET NULL;
-- modify "users" table
ALTER TABLE "users" DROP CONSTRAINT "users_files_file", DROP COLUMN "avatar_local_file", ADD CONSTRAINT "users_files_avatar_file" FOREIGN KEY ("avatar_local_file_id") REFERENCES "files" ("id") ON UPDATE NO ACTION ON DELETE SET NULL;

-- +goose Down
-- reverse: modify "users" table
ALTER TABLE "users" DROP CONSTRAINT "users_files_avatar_file", ADD COLUMN "avatar_local_file" character varying NULL, ADD CONSTRAINT "users_files_file" FOREIGN KEY ("avatar_local_file_id") REFERENCES "files" ("id") ON UPDATE NO ACTION ON DELETE SET NULL;
-- reverse: modify "organizations" table
ALTER TABLE "organizations" DROP CONSTRAINT "organizations_files_avatar_file", DROP COLUMN "avatar_local_file_id", DROP COLUMN "avatar_updated_at";
-- reverse: modify "user_history" table
ALTER TABLE "user_history" ADD COLUMN "avatar_local_file" character varying NULL;
-- reverse: modify "organization_history" table
ALTER TABLE "organization_history" DROP COLUMN "avatar_updated_at", DROP COLUMN "avatar_local_file_id";
3 changes: 2 additions & 1 deletion db/migrations-goose-postgres/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
h1:qOVARrtau4JhKV7gKSBoYBVqrfJctYhpjEu7VsnI0cY=
h1:I6+x6Xrpl6vRY8YmytqV1hS1C6AN5rIiydu0/AibHFo=
20241211231032_init.sql h1:Cj6GduEDECy6Y+8DfI6767WosqG2AKWybIyJJ6AgKqA=
20241212223714_consistent_naming.sql h1:RvnNmsStlHkmAdSCnX1fFh/KYgfj1RMYYEc4iCN9JcQ=
20250109002850_billing_address.sql h1:m0ek3WXqRgS3+ZbSa/DcIMB16vb8Tjm2MgNqyRll3rU=
20250109054654_remove_stripe_id.sql h1:OfeshKT2+ydfx+36e4uBrdQ31tuurcJsKXyYDp1ZiZg=
20250110162830_subscription_price.sql h1:ju8ocKPbqtOalaBZ1aGk8qCDLbgxFCDoA0Dmkr9bLzA=
20250110233413_orgsubsfeatures.sql h1:bEdXn+bDT7GuWRRkCYiFCK6t91DxjeEmkFWme2K/lCw=
20250114212723_avatar_files.sql h1:CmUGP2CRigFaGGhFdUt0uIDTOSy1HhIqao++18/KWsU=
8 changes: 8 additions & 0 deletions db/migrations/20250114212722_avatar_files.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Modify "organization_history" table
ALTER TABLE "organization_history" ADD COLUMN "avatar_local_file_id" character varying NULL, ADD COLUMN "avatar_updated_at" timestamptz NULL;
-- Modify "user_history" table
ALTER TABLE "user_history" DROP COLUMN "avatar_local_file";
-- Modify "organizations" table
ALTER TABLE "organizations" ADD COLUMN "avatar_updated_at" timestamptz NULL, ADD COLUMN "avatar_local_file_id" character varying NULL, ADD CONSTRAINT "organizations_files_avatar_file" FOREIGN KEY ("avatar_local_file_id") REFERENCES "files" ("id") ON UPDATE NO ACTION ON DELETE SET NULL;
-- Modify "users" table
ALTER TABLE "users" DROP CONSTRAINT "users_files_file", DROP COLUMN "avatar_local_file", ADD CONSTRAINT "users_files_avatar_file" FOREIGN KEY ("avatar_local_file_id") REFERENCES "files" ("id") ON UPDATE NO ACTION ON DELETE SET NULL;
3 changes: 2 additions & 1 deletion db/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
h1:FGyFOEh4ppoGd8KFzmyYNWHxBSlqreNG+TaZcAn068s=
h1:nnvTXfxLz/fAZVJmA5IH5Wq/G2NRRReWMDac4P6EMmY=
20241211231032_init.sql h1:TxjpHzKPB/5L2i7V2JfO1y+Cep/AyQN5wGjhY7saCeE=
20241212223712_consistent_naming.sql h1:tbdYOtixhW66Jmvy3aCm+X6neI/SRVvItKM0Bdn26TA=
20250109002849_billing_address.sql h1:mspCGbJ6HVmx3r4j+d/WvruzirJdJ8u5x18WF9R9ESE=
20250109054653_remove_stripe_id.sql h1:dB086/w/Ws7ZK8OvbRWX7ejN4HfizCKxcE3q/+Jv4U8=
20250110162828_subscription_price.sql h1:RlvrTtH6+PIFuJ+Fp5S06w0ho3B2O/Nn6QcWqSHLWEA=
20250110233411_orgsubsfeatures.sql h1:ZPBc/gedwoVMzJz6M/AN3yhtRmqdTuZsPvfIsWQj4fY=
20250114212722_avatar_files.sql h1:exooeELaAAu1jCnyjtok9WDC7WUxoPIeE6EBGFwhplU=
5 changes: 5 additions & 0 deletions internal/ent/generate/entc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/theopenlane/core/internal/ent/entconfig"
"github.com/theopenlane/core/pkg/entitlements"
"github.com/theopenlane/core/pkg/objects"

"github.com/theopenlane/core/internal/genhelpers"
)
Expand Down Expand Up @@ -123,6 +124,10 @@ func main() {
entc.DependencyName("EntitlementManager"),
entc.DependencyType(&entitlements.StripeClient{}),
),
entc.Dependency(
entc.DependencyName("ObjectManager"),
entc.DependencyType(&objects.Objects{}),
),
entc.TemplateDir("./internal/ent/templates"),
entc.Extensions(
gqlExt,
Expand Down
9 changes: 6 additions & 3 deletions internal/ent/generated/auditing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 31 additions & 3 deletions internal/ent/generated/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 86423d2

Please sign in to comment.