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

Ready: Row and field level security for users #401

Open
wants to merge 307 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
307 commits
Select commit Hold shift + click to select a range
430991d
Merge branch 'serializer-security' of https://github.com/hackforla/pe…
ethanstrominger Jul 5, 2024
23e80ca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2024
6dcadd4
Remove unneeded comments
ethanstrominger Jul 5, 2024
d706b1d
Merge branch 'serializer-security' of https://github.com/hackforla/pe…
ethanstrominger Jul 5, 2024
d73b307
Add pydoc
ethanstrominger Jul 6, 2024
8242a7e
Simplify
ethanstrominger Jul 6, 2024
7cfa7e9
Pydoc
ethanstrominger Jul 6, 2024
e6b8bbb
Refactor
ethanstrominger Jul 6, 2024
62d6bde
Refactor
ethanstrominger Jul 6, 2024
e143de1
Re-enable name testing check
ethanstrominger Jul 6, 2024
84cdad6
Refactor
ethanstrominger Jul 6, 2024
7cbd92a
refactor
ethanstrominger Jul 6, 2024
e55d101
Add pydoc comments, refactor
ethanstrominger Jul 6, 2024
c9de7fc
pydoc
ethanstrominger Jul 6, 2024
d17239d
Remove unused code in permissions.py
ethanstrominger Jul 6, 2024
3087e52
Revert changes to settings.py - no longer needed
ethanstrominger Jul 6, 2024
8128fb8
Refactor to extract function to PermissionUtil
ethanstrominger Jul 6, 2024
291f0c3
Remove unnecessary test
ethanstrominger Jul 6, 2024
263eff0
Refactor
ethanstrominger Jul 6, 2024
4a3fc54
Refactor
ethanstrominger Jul 6, 2024
26b6582
Refactor
ethanstrominger Jul 7, 2024
d3486a1
Modify a test
ethanstrominger Jul 7, 2024
9f71e69
Add tests for configurable
ethanstrominger Jul 7, 2024
c9aecee
Add create / post logic
ethanstrominger Jul 7, 2024
b66128c
Add post tests
ethanstrominger Jul 7, 2024
1fdacb0
Refactor to use FieldPermissions class
ethanstrominger Jul 7, 2024
34035ea
Fix pre-commit
ethanstrominger Jul 7, 2024
350f521
Skip post tests
ethanstrominger Jul 7, 2024
22446b8
Ignore name-tests-test checking in utils directory
ethanstrominger Jul 7, 2024
5739bd9
Implement a post test
ethanstrominger Jul 7, 2024
e126ef1
Get post tests and implementation working
ethanstrominger Jul 7, 2024
1aa45db
Default time_zone when creating
ethanstrominger Jul 7, 2024
f8d054c
test
ethanstrominger Jul 7, 2024
a510ac4
Test
ethanstrominger Jul 7, 2024
655aa7d
Test
ethanstrominger Jul 7, 2024
a767986
pre-commit comment out test
ethanstrominger Jul 7, 2024
88bafc4
pre-commit ignore makepath.sh and migrations dir
ethanstrominger Jul 8, 2024
03e5234
Figure out why test failing on push
ethanstrominger Jul 8, 2024
3e01768
Figure out why test failing on push
ethanstrominger Jul 8, 2024
a39fccf
Fix post tests
ethanstrominger Jul 8, 2024
c7e78e3
Refactor: change _update to _post_ and _create_ to _patch_
ethanstrominger Jul 8, 2024
2dae0d7
Refactor: change _update to _post_ and _create_ to _patch_
ethanstrominger Jul 8, 2024
8679624
Add comment
ethanstrominger Jul 8, 2024
2fc950b
Markdown explaining the flow of user field permission in the system.
ethanstrominger Jul 9, 2024
9e39528
Update user-field-permission-flow.md
ethanstrominger Jul 9, 2024
feaec66
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2024
390fde4
Update user-field-permission-flow.md
ethanstrominger Jul 9, 2024
d39d18f
Update user-field-permission-flow.md
ethanstrominger Jul 9, 2024
5cfe3d4
Update readme, refactor
ethanstrominger Jul 9, 2024
a7ca1db
Refactor
ethanstrominger Jul 9, 2024
09c004d
Remove unneeded test, configure black
ethanstrominger Jul 9, 2024
7550500
Medium refactor
ethanstrominger Jul 10, 2024
e7fa57d
Refactor
ethanstrominger Jul 10, 2024
779af13
Refactor
ethanstrominger Jul 10, 2024
6cccb1b
Refactor
ethanstrominger Jul 10, 2024
195e8ef
Refactor
ethanstrominger Jul 10, 2024
3143d2a
Refactor
ethanstrominger Jul 10, 2024
f2ba38c
Refactor
ethanstrominger Jul 10, 2024
eb62b39
Fix syntax
ethanstrominger Jul 10, 2024
8cd40fc
Rename FieldPermissions2 to FieldPermissions
ethanstrominger Jul 10, 2024
ea79b3e
Refactor tests
ethanstrominger Jul 10, 2024
b8709df
Modify pydoc comments
ethanstrominger Jul 10, 2024
ad4239f
Modify pydoc comments
ethanstrominger Jul 10, 2024
6e2a68d
Refactor and edit markdown
ethanstrominger Jul 11, 2024
fa41e4b
Update markdown
ethanstrominger Jul 11, 2024
3db6db5
Update markdown
ethanstrominger Jul 11, 2024
fd086b1
Merge branch 'user-permissions-22' into serializer-security
ethanstrominger Jul 12, 2024
82b8fe3
Merge branch 'serializer-security' of https://github.com/hackforla/pe…
ethanstrominger Jul 12, 2024
9d59810
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 12, 2024
e119388
Merge branch 'serializer-security' of https://github.com/hackforla/pe…
ethanstrominger Jul 12, 2024
c0f1f51
Resolve error after merge
ethanstrominger Jul 12, 2024
ce378b1
Rename UserProfileViewSet back to UserProfileAPIView
ethanstrominger Jul 12, 2024
e7a1d6c
Move load_data to tests dir
ethanstrominger Jul 13, 2024
0f0f1ae
Rename wanda_name and patrick_name
ethanstrominger Jul 13, 2024
a76bfcb
Remove unnecessary comments
ethanstrominger Jul 13, 2024
a57c8a8
Refactor and reduce tests
ethanstrominger Jul 13, 2024
1d44481
Add pydoc docs for tests and split into separate files
ethanstrominger Jul 13, 2024
e80cb9a
Split test_post_users into two files and pydoc
ethanstrominger Jul 13, 2024
773eaca
Implement documentation.py
ethanstrominger Jul 14, 2024
a07e674
Implement documentation.py
ethanstrominger Jul 14, 2024
110adfb
Implement documentation.py
ethanstrominger Jul 14, 2024
02a024f
Generate pydoc
ethanstrominger Jul 14, 2024
c9fbccd
Markdown, generte pydoc, and refactor
ethanstrominger Jul 14, 2024
34d31e5
Ignore manage.py for pydoc
ethanstrominger Jul 14, 2024
0e31d2d
Modify markup, remove unneeded files.
ethanstrominger Jul 14, 2024
36e8cfd
Merge main
ethanstrominger Sep 22, 2024
7643a45
Merge main
ethanstrominger Sep 22, 2024
0355c1e
Merge main
ethanstrominger Sep 22, 2024
1673005
Remove generated files
ethanstrominger Sep 22, 2024
8c10953
Prune files
ethanstrominger Sep 22, 2024
cf9f488
Clean up
ethanstrominger Sep 22, 2024
48a6f21
Clean up
ethanstrominger Sep 22, 2024
c3f40c4
Fix var naming
ethanstrominger Sep 22, 2024
acefbcf
WIP: test
ethanstrominger Sep 22, 2024
c304ba2
WIP
ethanstrominger Sep 23, 2024
5ddcb38
Fix test
ethanstrominger Sep 23, 2024
0c3709c
Fix test
ethanstrominger Sep 23, 2024
b6d828c
Skip failing tests
ethanstrominger Sep 23, 2024
7d46f75
Create user profile serializer
ethanstrominger Sep 23, 2024
ad585e3
Modify test_validate_fields_patchable_method
ethanstrominger Sep 23, 2024
b79f155
Modify test_validate_fields_patchable_method
ethanstrominger Sep 23, 2024
42f2f3e
Modify test_validate_fields_patchable_method
ethanstrominger Sep 23, 2024
fc5b0b6
Change is admin to look at user permissions rather than is_superuser
ethanstrominger Sep 24, 2024
214e0d9
Refactor - rename permission_util to permission_check
ethanstrominger Sep 24, 2024
f224e9e
Rename field_permissions to http_method_field_permissions.py
ethanstrominger Sep 24, 2024
9aa0611
Rename an attribute
ethanstrominger Sep 24, 2024
cfedac7
Update markdown
ethanstrominger Sep 25, 2024
8977c5f
Major refactoring of how privs are calculated
ethanstrominger Sep 25, 2024
6624435
Refactor
ethanstrominger Sep 25, 2024
1118cac
Remove unnecessary file
ethanstrominger Sep 25, 2024
0848067
Add description to cru_permissions
ethanstrominger Sep 25, 2024
fcf9d21
Get rid of unneeded file
ethanstrominger Sep 25, 2024
402edf1
Changes for pre-commit to add info for pytest
ethanstrominger Sep 25, 2024
16fc346
Test
ethanstrominger Sep 25, 2024
007f1c7
Debug messages
ethanstrominger Sep 25, 2024
1ddbe99
Fix test
ethanstrominger Sep 26, 2024
0581ead
Test
ethanstrominger Sep 26, 2024
6e8952f
Refactor to create cru class
ethanstrominger Sep 26, 2024
2ec944d
Update technical-details-of-permission-for-user-fields.md
ethanstrominger Oct 1, 2024
ec146fb
Update technical-details-of-permission-for-user-fields.md
ethanstrominger Oct 1, 2024
44361e3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2024
8b6189d
Update technical-details-of-permission-for-user-fields.md
ethanstrominger Oct 1, 2024
a648b45
remove unneeded file, update documentation
ethanstrominger Oct 1, 2024
34ecd22
md update
ethanstrominger Oct 1, 2024
0c6e68f
Remove self register logic
ethanstrominger Oct 2, 2024
efcce06
Merge technical doc
ethanstrominger Oct 3, 2024
73674f7
Restore .pre-commit-config.yaml
ethanstrominger Oct 5, 2024
7a2edad
Restore destroy description for user
ethanstrominger Oct 5, 2024
af3c4d0
Changes based on self review
ethanstrominger Oct 5, 2024
05ef4fa
Changes based on self review
ethanstrominger Oct 5, 2024
6454b58
Changes based on self review
ethanstrominger Oct 5, 2024
3f66f56
Changes based on self review
ethanstrominger Oct 5, 2024
42bcc93
Refactor
ethanstrominger Oct 5, 2024
6d203ed
Refactor
ethanstrominger Oct 5, 2024
ad05427
Refactor
ethanstrominger Oct 5, 2024
271175a
Refactor
ethanstrominger Oct 5, 2024
bd2ee42
Refactor
ethanstrominger Oct 5, 2024
303e545
Refactor
ethanstrominger Oct 5, 2024
7a5672f
Refactor
ethanstrominger Oct 5, 2024
2bf0c04
Refactor
ethanstrominger Oct 5, 2024
2c2eda4
Update doc
ethanstrominger Oct 5, 2024
b45438b
Speed up tests that require loading data
ethanstrominger Oct 7, 2024
cc4d81a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2024
d847cd4
Adjust for pre-commit errors
ethanstrominger Oct 7, 2024
4af990e
Merge branch 'serializer-security-346' of https://github.com/ethanstr…
ethanstrominger Oct 7, 2024
1c8122b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2024
fc0d12b
Adjust for pre-commit errors
ethanstrominger Oct 7, 2024
889b33d
Adjust for pre-commit errors
ethanstrominger Oct 7, 2024
4041ad7
Adjust for pre-commit errors
ethanstrominger Oct 7, 2024
9c05106
Suppress PT004 errors
ethanstrominger Oct 7, 2024
e9adc71
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2024
83928d7
Suppress PT004 errors
ethanstrominger Oct 7, 2024
39948af
Merge branch 'serializer-security-346' of https://github.com/ethanstr…
ethanstrominger Oct 7, 2024
54ea310
pre-commit errors
ethanstrominger Oct 7, 2024
beee99b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2024
8abb598
pre-commit errors
ethanstrominger Oct 7, 2024
4eb38c7
PT004 error
ethanstrominger Oct 7, 2024
00ecc36
PT004 error
ethanstrominger Oct 7, 2024
96ca7b1
Merge
ethanstrominger Oct 7, 2024
df538fd
Merge branch 'main' into serializer-security-346
ethanstrominger Oct 7, 2024
28937d9
Diagnose pre-commit check on github
ethanstrominger Oct 7, 2024
f6818b8
Diagnose pre-commit check on github
ethanstrominger Oct 7, 2024
4013398
xx
ethanstrominger Oct 7, 2024
51c520f
Merge branch 'main' of https://github.com/hackforla/peopledepot into …
ethanstrominger Oct 7, 2024
b45540c
precommit adjustments
ethanstrominger Oct 8, 2024
a2e69dd
Adjust setup.cfg
ethanstrominger Oct 8, 2024
f914011
Add pytest-xdist, fix conftest.py
ethanstrominger Oct 8, 2024
9cb774b
Refactor test scripts
ethanstrominger Oct 9, 2024
cee38c5
Speed up test.sh
ethanstrominger Oct 9, 2024
cff8069
Refactor
ethanstrominger Oct 9, 2024
f72a6a5
Misc changes
ethanstrominger Oct 10, 2024
0839da4
Updates to technical details doc
ethanstrominger Oct 10, 2024
b048244
Refactor code
ethanstrominger Oct 13, 2024
9b1c3f6
Restore earlier version
ethanstrominger Oct 13, 2024
a455dfa
Merge branch 'serializer-security-346' of https://github.com/ethanstr…
ethanstrominger Oct 13, 2024
6878b08
Refactor
ethanstrominger Oct 13, 2024
de1adfc
Refactor
ethanstrominger Oct 13, 2024
9320b6c
Merge branch 'serializer-security-346' of https://github.com/hackforl…
ethanstrominger Oct 16, 2024
58819da
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
c3401e9
permission_check WIP
ethanstrominger Oct 24, 2024
016731c
Complete test_get_most_privileged_perm_type
ethanstrominger Oct 25, 2024
33aefde
Implement more permission test checks with parameters for test
ethanstrominger Oct 27, 2024
d033f1a
Refactor
ethanstrominger Oct 27, 2024
c8b4abc
Fix validate_fields
ethanstrominger Oct 27, 2024
7455a7b
Got more tests to pass
ethanstrominger Oct 28, 2024
a35bcd9
Remove cache, fix failing test
ethanstrominger Oct 28, 2024
1859ed1
Update permission "UserMethodPermission"), create a test for user pat…
ethanstrominger Oct 28, 2024
2d02f59
Add tests that call the API through a web request
ethanstrominger Oct 29, 2024
564902f
Refactor: WIP
ethanstrominger Oct 30, 2024
d86a84a
Refactor
ethanstrominger Oct 30, 2024
43ee51c
Refactor
ethanstrominger Oct 30, 2024
e2fb91b
Remove commented out code
ethanstrominger Oct 30, 2024
9bfd282
Refactor
ethanstrominger Oct 30, 2024
852504a
Refactor, fix test_patch_user.py
ethanstrominger Oct 30, 2024
a0e8cb7
Refactor, fix test_patch_user.py
ethanstrominger Oct 30, 2024
ccec09b
Got all tests to pass or marked as skip
ethanstrominger Oct 31, 2024
fae07e4
Got all tests to pass
ethanstrominger Oct 31, 2024
5a255f5
Add profile to field_permissions
ethanstrominger Oct 31, 2024
dead59f
All profile and user tests now working
ethanstrominger Nov 1, 2024
29e33fc
Revert profile changes
ethanstrominger Nov 1, 2024
e737c06
Merge branch 'serializer-security-346' of https://github.com/hackforl…
ethanstrominger Nov 1, 2024
49e9ed9
CONTRIBUTING.md restore
ethanstrominger Nov 1, 2024
eca2f91
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2024
8d6bc6b
Delete app/core/api/flow.md
ethanstrominger Nov 1, 2024
4c2753f
Delete app/core/tests/field_permissions.csv
ethanstrominger Nov 1, 2024
56d6cd7
Changes for pre-commit
ethanstrominger Nov 1, 2024
d7bb7fb
Minor refactor
ethanstrominger Nov 1, 2024
1d8a7be
pre-commit changes
ethanstrominger Nov 1, 2024
c61e166
pre-commit changes
ethanstrominger Nov 1, 2024
bdbc6bc
Merge branch 'serializer-security-346' of https://github.com/hackforl…
ethanstrominger Nov 1, 2024
730def1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2024
4894876
pre-commit and duplicate code
ethanstrominger Nov 1, 2024
d7c7c52
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2024
eeef299
change for pre-commit
ethanstrominger Nov 1, 2024
e69e71f
Refactor: simplify
ethanstrominger Nov 1, 2024
ff89ae2
Formatting
ethanstrominger Nov 1, 2024
2aa9b5a
Change shell scripts for pre-commit
ethanstrominger Nov 1, 2024
401d387
Remove unused import
ethanstrominger Nov 1, 2024
ef0de04
Fix path.open syntax
ethanstrominger Nov 1, 2024
18ba588
Formatting
ethanstrominger Nov 1, 2024
1c663ff
Modify for pre-commit
ethanstrominger Nov 1, 2024
80b1c59
Add noqa for pre-commit
ethanstrominger Nov 1, 2024
0f134fc
Complete merge
ethanstrominger Nov 1, 2024
2833252
Refactor
ethanstrominger Nov 16, 2024
94eafa9
Refactor, add new class generic_request
ethanstrominger Nov 16, 2024
e26f639
Working
ethanstrominger Nov 16, 2024
76199ab
Fix bugs - func and tests
ethanstrominger Nov 17, 2024
da3c729
Fix errors in tests
ethanstrominger Nov 17, 2024
93037f3
Merge branch 'main' into serializer-security-346
ethanstrominger Nov 17, 2024
d8bd7d5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 17, 2024
ea5f21f
Fix conflict resolution errors
ethanstrominger Nov 18, 2024
b5f651c
Merge branch 'serializer-security-346' of https://github.com/hackforl…
ethanstrominger Nov 18, 2024
62d8058
Revert from test_post to original test_create
ethanstrominger Nov 18, 2024
6d2bc1c
Remove unnecessary titles from field_permissions.csv
ethanstrominger Nov 18, 2024
c40230e
Refactor tests, document security implementation
ethanstrominger Nov 19, 2024
f86a75a
Update markdown
ethanstrominger Nov 19, 2024
cfba26f
Create how to implement user based security markdown.
ethanstrominger Nov 19, 2024
8b3ece3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 19, 2024
20a22aa
Update how to guide
ethanstrominger Nov 19, 2024
e099d87
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 19, 2024
6c75611
Document how to implement security
ethanstrominger Nov 20, 2024
9efdd68
Merge branch 'serializer-security-346' of https://github.com/hackforl…
ethanstrominger Nov 20, 2024
1140ef8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 20, 2024
5a1ddd3
fix doc headers and indentation
fyliu Nov 20, 2024
883a32d
Remove unused import
ethanstrominger Dec 1, 2024
399791f
Merge branch 'serializer-security-346' of https://github.com/hackforl…
ethanstrominger Dec 1, 2024
2994ce7
Fix update-table.md
ethanstrominger Dec 1, 2024
3de5de4
Trigger pre-commit again
ethanstrominger Dec 1, 2024
bb25137
Merge remote-tracking branch 'upstream/main' into serializer-security…
fyliu Dec 3, 2024
0463537
style: mdformat fixes
fyliu Dec 3, 2024
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
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/update-table.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: Update Table
about: Describe this issue template's purpose here.
about: Describe the purpose of the issue template here
title: 'Update Table: [TABLE NAME]'
labels: 'feature: update table, good first issue, milestone: missing, role: back end,
size: 0.25pt, stakeholder: missing'
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ repos:
- id: fix-byte-order-marker
- id: name-tests-test
args: [--pytest-test-first]

exclude: ^app/core/tests/utils/
# general quality checks
- id: mixed-line-ending
- id: trailing-whitespace
Expand Down
4 changes: 2 additions & 2 deletions app/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
global_admin = "globalAdmin"
admin_global = "adminGlobal"
admin_project = "adminProject"
practice_lead_project = "practiceLeadProject"
member_project = "memberProject"
self_value = "self"
field_permissions_csv_file = "core/api/field_permissions.csv"
54 changes: 54 additions & 0 deletions app/core/api/field_permissions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
table_name,field_name,get,patch,post
User,username,memberProject,,adminGlobal
User,is_active,,,
User,is_staff,,,
User,first_name,memberProject,adminBrigade,adminGlobal
User,last_name,memberProject,adminBrigade,adminGlobal
User,gmail,practiceLeadProject,adminBrigade,adminGlobal
User,preferred_email,practiceLeadProject,adminBrigade,adminGlobal
User,created_at,adminProject,,,
User,user_status_id,adminBrigade,adminBrigade,adminGlobal
User,current_job_title,adminBrigade,adminBrigade,adminGlobal
User,target_job_title,adminBrigade,adminBrigade,adminGlobal
User,current_skills,adminBrigade,adminBrigade,adminGlobal
User,target_skills,adminBrigade,adminBrigade,adminGlobal
User,linkedin_account,memberProject,adminBrigade,adminGlobal
User,github_handle,memberProject,adminBrigade,adminGlobal
User,phone,practiceLeadProject,adminBrigade,adminGlobal
User,texting_ok,practiceLeadProject,adminBrigade,adminGlobal
User,slack_id,memberProject,adminBrigade,adminGlobal
User,time_zone,memberProject,adminBrigade,adminGlobal
User,last_updated,adminBrigade,,adminGlobal
User,password,,adminBrigade,adminGlobal,,,,
UserPermission,field1,memberProject,adminProject,adminProject

John Smith adminGlobal

Wanda adminProject website-project
Wally memberProject website-project
Paul memberProjbect peopledepot-project

If Wanda tries to read Wally, the highest privilege is adminProject for Wanda, gmail should be includded
If Wanda tries to read Paul, there is no project in common, the highest privilege is None

UserPermissions
user

blackBox(logged in user, self.user, "UserPermission")


blackBox(logged in user, target user, table)

UserOpportunities
user
blackBox(logged in user, self.user, "UserPermission")


User =< UserEvent =< UserAttendance

UserEvent
user

UserAttendance
user userEvent.user
userEvent
145 changes: 145 additions & 0 deletions app/core/api/permission_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import csv
from pathlib import Path
from typing import Any

from rest_framework.exceptions import PermissionDenied

from constants import admin_global # Assuming you have this constant
from constants import field_permissions_csv_file
from core.models import PermissionType
from core.models import UserPermission


class PermissionValidation:
@staticmethod
def is_admin(user) -> bool:
"""Check if a user has admin permissions."""
permission_type = PermissionType.objects.filter(name=admin_global).first()
# return True
return UserPermission.objects.filter(
permission_type=permission_type, user=user
).exists()

@staticmethod
def get_rank_dict() -> dict[str, int]:
"""Return a dictionary mapping permission names to their ranks."""
permissions = PermissionType.objects.values("name", "rank")
return {perm["name"]: perm["rank"] for perm in permissions}

@staticmethod
def get_csv_field_permissions() -> dict[str, dict[str, list[dict[str, Any]]]]:
"""Read the field permissions from a CSV file."""
file_path = Path(field_permissions_csv_file)
with file_path.open() as file:
reader = csv.DictReader(file)
return list(reader)

@classmethod
def get_fields(
cls, operation: str, permission_type: str, table_name: str
) -> list[str]:
"""Return the valid fields for the given permission type."""

valid_fields = []
if permission_type == "":
return valid_fields
for field in cls.get_csv_field_permissions():
if cls.is_field_valid(
operation=operation,
permission_type=permission_type,
table_name=table_name,
field=field,
):
valid_fields += [field["field_name"]]
return valid_fields

@classmethod
def get_fields_for_post_request(cls, request, table_name):
requesting_user = request.user
if not cls.is_admin(requesting_user):
raise PermissionDenied("You do not have privilges to create.")
fields = cls.get_fields(
operation="post",
table_name=table_name,
permission_type=admin_global,
)
return fields

@classmethod
def get_fields_for_patch_request(cls, request, table_name, response_related_user):
requesting_user = request.user
requesting_user = request.user
most_privileged_perm_type = cls.get_most_privileged_perm_type(
requesting_user, response_related_user
)
fields = cls.get_fields(
operation="patch",
table_name=table_name,
permission_type=most_privileged_perm_type,
)
return fields

@classmethod
def get_fields_for_response(cls, request, table_name, response_related_user):
requesting_user = request.user
most_privileged_perm_type = cls.get_most_privileged_perm_type(
requesting_user, response_related_user
)
fields = cls.get_fields(
operation="get",
table_name=table_name,
permission_type=most_privileged_perm_type,
)
return fields

@classmethod
def get_most_privileged_perm_type(
cls, requesting_user, response_related_user
) -> str:
"""Return the most privileged permission type between users."""
if cls.is_admin(requesting_user):
return admin_global

target_projects = UserPermission.objects.filter(
user=response_related_user
).values_list("project__name", flat=True)
target_projects = UserPermission.objects.filter(
user=response_related_user
).values_list("project__name", flat=True)

permissions = UserPermission.objects.filter(
user=requesting_user, project__name__in=target_projects
).values("permission_type__name", "permission_type__rank")

if not permissions:
return ""

min_permission = min(permissions, key=lambda p: p["permission_type__rank"])
return min_permission["permission_type__name"]

@classmethod
def get_response_fields(cls, request, table_name, response_related_user) -> None:
"""Ensure the requesting user can patch the provided fields."""
requesting_user = request.user
most_privileged_perm_type = cls.get_most_privileged_perm_type(
requesting_user, response_related_user
)
fields = cls.get_fields(
operation="get",
table_name=table_name,
permission_type=most_privileged_perm_type,
)
return fields

@classmethod
def is_field_valid(
cls, operation: str, permission_type: str, table_name: str, field: dict
):
operation_permission_type = field[operation]
if operation_permission_type == "" or field["table_name"] != table_name:
return False
rank_dict = cls.get_rank_dict()
source_rank = rank_dict[permission_type]
source_rank = rank_dict[permission_type]
rank_match = source_rank <= rank_dict[operation_permission_type]
return rank_match
20 changes: 18 additions & 2 deletions app/core/api/permissions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
from rest_framework.permissions import BasePermission

from core.api.user_related_request import UserRelatedRequest


class DenyAny(BasePermission):
def has_permission(self, request, view):
def has_permission(self, __request__, __view__):
return False

def has_object_permission(self, request, view, obj):
def has_object_permission(self, __request__, __view__, __obj__):
return False


class GenericPermission(BasePermission):
def has_permission(self, request, view):
if request.method == "POST":
UserRelatedRequest.validate_post_fields(request=request, view=view)
return True # Default to allow the request

def has_object_permission(self, request, view, obj):
if request.method == "PATCH":
UserRelatedRequest.validate_patch_fields(
view=view, obj=obj, request=request
)
return True
48 changes: 48 additions & 0 deletions app/core/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework import serializers
from timezone_field.rest_framework import TimeZoneSerializerField

from core.api.user_related_request import UserRelatedRequest
from core.models import Affiliate
from core.models import Affiliation
from core.models import CheckType
Expand Down Expand Up @@ -68,13 +69,60 @@ class UserSerializer(serializers.ModelSerializer):

time_zone = TimeZoneSerializerField(use_pytz=False)

def to_representation(self, instance):
representation = super().to_representation(instance)
return UserRelatedRequest.get_serializer_representation(
self, instance, representation
)

class Meta:
model = User
fields = (
"uuid",
"username",
"created_at",
"updated_at",
"is_superuser",
"is_active",
"is_staff",
"email",
"first_name",
"last_name",
"gmail",
"preferred_email",
"current_job_title",
"target_job_title",
"current_skills",
"target_skills",
"linkedin_account",
"github_handle",
"slack_id",
"phone",
"texting_ok",
"time_zone",
)
read_only_fields = (
"uuid",
"created_at",
"updated_at",
"username",
"email",
)


class UserProfileSerializer(serializers.ModelSerializer):
time_zone = TimeZoneSerializerField(use_pytz=False)

class Meta:
model = User
fields = (
"uuid",
"username",
"created_at",
"updated_at",
"is_superuser",
"is_active",
"is_staff",
"email",
"first_name",
"last_name",
Expand Down
Loading