Coverage for src/edwh_auth_rbac/migrations.py: 100%

1 from edwh_migrate import migration

2

3

4@migration()

5 def rbac_migrations(db):

6 db.executesql("""

7 drop view if exists recursive_memberships;

8 """)

9 db.executesql("""

10 create view recursive_memberships as

11 -- each root is member of object_id, including one line for himself.

12 -- also for a user

13 with RECURSIVE m(root, object_id, object_type, level, email, firstname, fullname) as (

14 select object_id as root, object_id, object_type, 0, email, firstname, fullname

15 from identity

16 union all

17 select root, membership.member_of, i.object_type, m.level+1, i.email, i.firstname, i.fullname

18 from membership join m on subject == m.object_id

19 join identity i on i.object_id = membership.member_of

20 order by root, m.level+1

21 )

22 select * from m

23;

24""")

25

26 db.executesql("""

27 drop view if exists recursive_members;

28

29 """)

30

31 db.executesql("""

32 create view recursive_members as

33 with RECURSIVE m(root, object_id, object_type, level, email, firstname, fullname) as (

34 select object_id as root, object_id, object_type, 0, email, firstname, fullname

35 from identity

36 union all

37 select root, membership.subject, i.object_type, m.level+1, i.email, i.firstname, i.fullname

38 from membership join m on member_of== m.object_id

39 join identity i on i.object_id = membership.subject

40 order by root

41 )

42 select * from m

43;

44

45 """)

46

47 db.commit()

48 return True