Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DA-212: Rename file/folder backend endpoints are ready. #201

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 138 additions & 5 deletions app/mainapp/graphql/private/generated.go

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

9 changes: 8 additions & 1 deletion app/mainapp/graphql/private/resolvers/code_editor.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extend type Mutation {
+ **Route**: Private
+ **Permissions**: admin_platform, platform_environment, specific_pipeline[write]
"""
renameFolder(environmentID: String!, folderID: String!, nodeID: String!, pipelineID: String!): String!
renameFolder(environmentID: String!, folderID: String!, nodeID: String!, pipelineID: String!, newName: String!): String!

"""
Upload a node file.
Expand All @@ -87,7 +87,14 @@ extend type Mutation {
"""
deleteFileNode(environmentID: String!, fileID: String!, nodeID: String!, pipelineID: String!): String!

"""
Rename a file.
+ **Route**: Private
+ **Permissions**: admin_platform, platform_environment, specific_pipeline[write]
"""
renameFile(environmentID: String!, fileID: String!, nodeID: String!, pipelineID: String!, newName: String!): String!

"""
Move a file.
+ **Route**: Private
+ **Permissions**: admin_platform, platform_environment, specific_pipeline[write]
Expand Down
100 changes: 96 additions & 4 deletions app/mainapp/graphql/private/resolvers/code_editor.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
privategraphql "dataplane/mainapp/graphql/private"
"dataplane/mainapp/logging"
"errors"
"fmt"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -109,7 +108,7 @@ func (r *mutationResolver) MoveFolderNode(ctx context.Context, folderID string,

// Update folder's parent in the database
err = database.DBConn.Model(&models.CodeFolders{}).
Where("folder_id = ?", folderID).Update("parent_id", toFolderID).Error
Where("folder_id = ? and environment_id = ?", folderID, environmentID).Update("parent_id", toFolderID).Error
if err != nil {
return "", errors.New(err.Error())
}
Expand Down Expand Up @@ -225,8 +224,52 @@ func (r *mutationResolver) DeleteFolderNode(ctx context.Context, environmentID s
return "Success", nil
}

func (r *mutationResolver) RenameFolder(ctx context.Context, environmentID string, folderID string, nodeID string, pipelineID string) (string, error) {
panic(fmt.Errorf("not implemented"))
func (r *mutationResolver) RenameFolder(ctx context.Context, environmentID string, folderID string, nodeID string, pipelineID string, newName string) (string, error) {
currentUser := ctx.Value("currentUser").(string)
platformID := ctx.Value("platformID").(string)

// ----- Permissions
perms := []models.Permissions{
{Subject: "user", SubjectID: currentUser, Resource: "admin_platform", ResourceID: platformID, Access: "write", EnvironmentID: "d_platform"},
{Subject: "user", SubjectID: currentUser, Resource: "platform_environment", ResourceID: platformID, Access: "write", EnvironmentID: environmentID},
{Subject: "user", SubjectID: currentUser, Resource: "environment_edit_all_pipelines", ResourceID: platformID, Access: "write", EnvironmentID: environmentID},
{Subject: "user", SubjectID: currentUser, Resource: "specific_pipeline", ResourceID: pipelineID, Access: "write", EnvironmentID: environmentID},
}

permOutcome, _, _, _ := permissions.MultiplePermissionChecks(perms)

if permOutcome == "denied" {
return "", errors.New("Requires permissions.")
}

// Get parent's folder id
f := models.CodeFolders{}
err := database.DBConn.Where("folder_id = ? and environment_id = ?", folderID, environmentID).Find(&f).Error
if err != nil {
return "", errors.New(err.Error())
}

parentFolderpath, _ := filesystem.FolderConstructByID(database.DBConn, f.ParentID, environmentID)
folderpath, _ := filesystem.FolderConstructByID(database.DBConn, folderID, environmentID)

// Make sure there is a path
if strings.TrimSpace(folderpath) == "" || strings.TrimSpace(parentFolderpath) == "" {
return "", errors.New("Missing folder path.")
}

// 1. ----- Rename folder in the directory
err = os.Rename(config.CodeDirectory+folderpath, config.CodeDirectory+parentFolderpath+folderID+"_"+newName)
if err != nil {
return "", errors.New(err.Error())
}

// 2. ----- Rename folder in the database
err = database.DBConn.Model(&models.CodeFolders{}).Where("folder_id = ? and environment_id = ?", folderID, environmentID).Update("folder_name", newName).Error
if err != nil {
return "", errors.New(err.Error())
}

return "Success", nil
}

func (r *mutationResolver) UploadFileNode(ctx context.Context, environmentID string, nodeID string, pipelineID string, folderID string, file graphql.Upload) (string, error) {
Expand All @@ -253,6 +296,7 @@ func (r *mutationResolver) UploadFileNode(ctx context.Context, environmentID str
file.File.Read(p)

input := models.CodeFiles{
PipelineID: pipelineID,
EnvironmentID: environmentID,
NodeID: nodeID,
FileName: file.Filename,
Expand Down Expand Up @@ -363,6 +407,54 @@ func (r *mutationResolver) DeleteFileNode(ctx context.Context, environmentID str
return "Success", nil
}

func (r *mutationResolver) RenameFile(ctx context.Context, environmentID string, fileID string, nodeID string, pipelineID string, newName string) (string, error) {
currentUser := ctx.Value("currentUser").(string)
platformID := ctx.Value("platformID").(string)

// ----- Permissions
perms := []models.Permissions{
{Subject: "user", SubjectID: currentUser, Resource: "admin_platform", ResourceID: platformID, Access: "write", EnvironmentID: "d_platform"},
{Subject: "user", SubjectID: currentUser, Resource: "platform_environment", ResourceID: platformID, Access: "write", EnvironmentID: environmentID},
{Subject: "user", SubjectID: currentUser, Resource: "environment_edit_all_pipelines", ResourceID: platformID, Access: "write", EnvironmentID: environmentID},
{Subject: "user", SubjectID: currentUser, Resource: "specific_pipeline", ResourceID: pipelineID, Access: "write", EnvironmentID: environmentID},
}

permOutcome, _, _, _ := permissions.MultiplePermissionChecks(perms)

if permOutcome == "denied" {
return "", errors.New("Requires permissions.")
}

// Get parent's folder id
f := models.CodeFiles{}
err := database.DBConn.Where("file_id = ? and environment_id = ?", fileID, environmentID).Find(&f).Error
if err != nil {
return "", errors.New(err.Error())
}

folderpath, _ := filesystem.FolderConstructByID(database.DBConn, f.FolderID, environmentID)
filepath, _ := filesystem.FileConstructByID(database.DBConn, fileID, environmentID)

// Make sure there is a path
if strings.TrimSpace(filepath) == "" || strings.TrimSpace(folderpath) == "" {
return "", errors.New("Missing folder path.")
}

// // 1. ----- Rename file in the directory
err = os.Rename(config.CodeDirectory+filepath, config.CodeDirectory+folderpath+newName)
if err != nil {
return "", errors.New(err.Error())
}

// // 2. ----- Rename file in the database
err = database.DBConn.Model(&models.CodeFiles{}).Where("file_id = ? and environment_id = ?", fileID, environmentID).Update("file_name", newName).Error
if err != nil {
return "", errors.New(err.Error())
}

return "Success", nil
}

func (r *mutationResolver) MoveFileNode(ctx context.Context, fileID string, toFolderID string, environmentID string, pipelineID string) (string, error) {
currentUser := ctx.Value("currentUser").(string)
platformID := ctx.Value("platformID").(string)
Expand Down