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

feat(api): implement permissions system #927

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jordy2254
Copy link
Member

No description provided.

@jordy2254 jordy2254 added feature A new idea or feature API Related to the backend api server written in Go labels Apr 1, 2024
@jordy2254 jordy2254 self-assigned this Apr 1, 2024
@jordy2254 jordy2254 marked this pull request as ready for review April 1, 2024 18:46
Copy link

codecov bot commented Apr 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.00%. Comparing base (b2d591b) to head (33e9336).
Report is 3 commits behind head on master.

❗ Current head 33e9336 differs from pull request most recent head 2d30ece. Consider uploading reports for the commit 2d30ece to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           master     #927       +/-   ##
===========================================
+ Coverage   57.34%   70.00%   +12.65%     
===========================================
  Files         196      100       -96     
  Lines       15607    11200     -4407     
  Branches      533      533               
===========================================
- Hits         8950     7840     -1110     
+ Misses       6408     3360     -3048     
+ Partials      249        0      -249     
Flag Coverage Δ
api ?
ui 70.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jordy2254 jordy2254 marked this pull request as draft April 1, 2024 18:53
Copy link
Member

@viktorstrate viktorstrate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a problem with the database_models in database.go, see comment.

@@ -153,8 +154,13 @@ func SetupDatabase() (*gorm.DB, error) {
return db, nil
}

var database_models []interface{} = []interface{}{
var database_models_1 []interface{} = []interface{}{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tests don't work since you make two database models and try to migrate them both. Why did you add a database_models_1?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@viktorstrate the reason for this was due to some query size constraints atleast within mysql. I was going to investigate further to find a better solution. I will take a further look st this and hope its the cause I'd the failures

)

// MigrateForPermissions Migrates new permissions into the system and upgrades system roles according to the new permissions.
func MigrateForPermissions(db *gorm.DB) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test this migration thoroughly before releasing, since it is a little more involved.
Just to make sure a lot of users databases doesn't get corrupted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully agree on this one, I've tested the scenarios I can think of albeit on a small dataset and only a few user's. These are as below.

  • Migrating from an old version of photoview with various admin users, normal user & demo user
  • Addition of a new permission
  • Addition of an existing permission to a system user group doesn't add to the user group
  • Addition of a new permission to a system user group automatically adds to the user group

admin: Boolean
): User! @isAdmin
roleId: ID
admin: Boolean @deprecated(reason: "The roles system replaces the old admin boolean.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like that you deprecated the old method. Then when we want to make a 3.0 release at some point, we can refactor out all the deprecations.

@@ -1,9 +1,16 @@
directive @isAuthorized on FIELD_DEFINITION
directive @isAdmin on FIELD_DEFINITION
directive @hasPermission(permission: Permission!) on FIELD_DEFINITION
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a @hasPermission directive is awesome :)

@jordy2254
Copy link
Member Author

Just an update for anyone interested on this. First of all I'll need some user's to test migrations if anyone see's this and is interested ping me a message on discord.

I've not had much time to look at this as I had hoped but finally managed to get a look this afternoon at some of the test faliures and it's not mostly working. below is what's left/what I've changed since last pushing

  • The test suite is now nearly fixed, I've implemented database transactions for all tests rather than the delete everything system for the end of a test that was implemented before.
  • I have one more test set to fix which is the scanner tests, this is because of the scanner creating a transaction per thread which locks the outer transaction causing errors, I'm yet to investigate this one but have some ideas.
  • UI Changes still need doing the initial pass of these will be the ability to select a user role for a user only and not create and amend the roles themselves.

I'm hoping I'll be able to finish this off during the week depending on how much dev time I get but I won't promise that just yet.

@@ -104,7 +127,8 @@ type Query {
type Mutation {
"Authorizes a user and returns a token used to identify the new session"
authorizeUser(username: String!, password: String!): AuthorizeResult!

createRole(role: NewRoleInput): Role!
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Missing permission derectives on these 2 mutations
  • Need to implement a delete mutation

api/api Outdated Show resolved Hide resolved
items.push({ label: 'Loading...', value: '' })
} else {
if (!data?.roles.find(role => role.id === props.selected)) {
items.push({ label: 'Please Select', value: '' })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing translation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Related to the backend api server written in Go feature A new idea or feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants