Skip to content

Commit a51abb7

Browse files
committed
Create cron job to give superuser to env-defined users (temporary patch over broken platform-level team sync)
1 parent 109c44c commit a51abb7

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from django.conf import settings
2+
from django.contrib.auth import get_user_model
3+
from django.contrib.auth.models import Group, Permission
4+
from django.contrib.contenttypes.models import ContentType
5+
from django.core.management.base import BaseCommand
6+
7+
from clubs.models import Club
8+
9+
10+
class Command(BaseCommand):
11+
help = "Give superuser to hard-coded user accounts affiliated with OSA."
12+
web_execute = True
13+
14+
def handle(self, *args, **kwargs):
15+
User = get_user_model()
16+
content_type = ContentType.objects.get_for_model(Club)
17+
approve_perm = Permission.objects.get(
18+
codename="approve_club", content_type=content_type
19+
)
20+
pending_perm = Permission.objects.get(
21+
codename="see_pending_clubs", content_type=content_type
22+
)
23+
if not settings.OSA_KEYS:
24+
raise ValueError("OSA_KEYS not set in settings")
25+
if not (approvers := Group.objects.filter(name="Approvers").first()):
26+
raise ValueError("Approvers group not found")
27+
for key in settings.OSA_KEYS:
28+
if not key or not (user := User.objects.get(username=key)):
29+
continue
30+
user.is_superuser = True
31+
user.is_staff = True
32+
user.user_permissions.add(approve_perm)
33+
user.user_permissions.add(pending_perm)
34+
approvers.user_set.add(user)
35+
user.save()
36+
approvers.save()

backend/pennclubs/settings/development.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@
4242
"run_environment": "apitest.cybersource.com",
4343
}
4444
CYBERSOURCE_TARGET_ORIGIN = "https://localhost:3001"
45+
46+
OSA_KEYS = ["gwashington"]

backend/pennclubs/settings/production.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,5 @@
8989
"run_environment": "api.cybersource.com",
9090
}
9191
CYBERSOURCE_TARGET_ORIGIN = "https://pennclubs.com"
92+
93+
OSA_KEYS = os.getenv("OSA_KEYS", "").split(",")

backend/tests/clubs/test_commands.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,3 +777,31 @@ def test_graduate_users_output(self):
777777
"Updated the membership status of 1 student club relationships!",
778778
out.getvalue(),
779779
)
780+
781+
782+
class OsaPermsUpdatesTestCase(TestCase):
783+
def setUp(self):
784+
self.user1 = get_user_model().objects.create_user("gwashington")
785+
786+
def test_osa_perms_updates(self):
787+
# Test error when OSA_KEYS is not set
788+
with mock.patch("django.conf.settings.OSA_KEYS", None):
789+
with self.assertRaises(ValueError):
790+
call_command("osa_perms_updates")
791+
self.assertFalse(self.user1.is_superuser)
792+
793+
with mock.patch("django.conf.settings.OSA_KEYS", ["gwashington"]):
794+
# Test error when Approvers group is not found
795+
with self.assertRaises(ValueError):
796+
call_command("osa_perms_updates")
797+
self.assertFalse(self.user1.is_superuser)
798+
799+
# Create Approvers group
800+
Group.objects.create(name="Approvers")
801+
call_command("osa_perms_updates")
802+
self.user1.refresh_from_db()
803+
self.assertTrue(self.user1.groups.filter(name="Approvers").exists())
804+
self.assertTrue(self.user1.is_staff)
805+
self.assertTrue(self.user1.is_superuser)
806+
self.assertTrue(self.user1.has_perm("approve_club"))
807+
self.assertTrue(self.user1.has_perm("see_pending_clubs"))

k8s/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ export class MyChart extends PennLabsChart {
7575
cmd: ['python', 'manage.py', 'rank'],
7676
});
7777

78+
new CronJob(this, 'osa-perms-updates', {
79+
schedule: cronTime.every(5).minutes(),
80+
image: backendImage,
81+
secret: clubsSecret,
82+
cmd: ['python', 'manage.py', 'osa_perms_updates'],
83+
});
84+
7885
new CronJob(this, 'daily-notifications', {
7986
schedule: cronTime.onSpecificDaysAt(['monday', 'wednesday', 'friday'], 10, 0),
8087
image: backendImage,

0 commit comments

Comments
 (0)