Skip to content

Commit

Permalink
add ablity to add rows to permission table
Browse files Browse the repository at this point in the history
  • Loading branch information
Akopti8 committed Jul 11, 2024
1 parent 9759eeb commit 0242afb
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions auth/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Database interface {
CheckUserPermission(userEmail, bucket, prefix string, operations []string) bool
Close() error
GetUserAccessiblePrefixes(userEmail, bucket string, operations []string) ([]string, error)
AddBucketPermissions(userEmail, bucket string, prefixes []string, operation string) error
}

type PostgresDB struct {
Expand Down Expand Up @@ -123,6 +124,26 @@ func (db *PostgresDB) CheckUserPermission(userEmail, bucket, prefix string, oper
return hasPermission
}

// AddBucketPermissions adds permissions for a user to access specific prefixes in a bucket for a given operation.
func (db *PostgresDB) AddBucketPermissions(userEmail, bucket string, prefixes []string, operation string) error {
allowedPrefixes := make([]string, len(prefixes))
for i, prefix := range prefixes {
allowedPrefixes[i] = fmt.Sprintf("/%s/%s", bucket, prefix)
}

query := `
INSERT INTO permissions (user_email, operation, allowed_s3_prefixes)
VALUES ($1, $2, $3);
`

_, err := db.Handle.Exec(query, userEmail, operation, pq.Array(allowedPrefixes))
if err != nil {
return fmt.Errorf("error adding bucket permissions: %v", err)
}

return nil
}

// Close closes the database connection.
func (db *PostgresDB) Close() error {
return db.Handle.Close()
Expand Down

0 comments on commit 0242afb

Please sign in to comment.