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 """

8 drop view if exists recursive_memberships;

9 """

10 )

11 db.executesql(

12 """

13 create view recursive_memberships as

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

15 -- also for a user

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

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

18 from identity

19 union all

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

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

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

23 order by root, m.level+1

24 )

25 select * from m

26;

27"""

28 )

29

30 db.executesql(

31 """

32 drop view if exists recursive_members;

33

34 """

35 )

36

37 db.executesql(

38 """

39 create view recursive_members as

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

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

42 from identity

43 union all

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

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

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

47 order by root

48 )

49 select * from m

50;

51

52 """

53 )

54

55 db.commit()

56 return True