group_management.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. use crate::{schema, UIDCError};
  2. use microrm::prelude::*;
  3. pub fn create_group(
  4. qi: &microrm::QueryInterface,
  5. realm_id: schema::RealmID,
  6. name: &str,
  7. ) -> Result<(), UIDCError> {
  8. qi.add(&schema::Group {
  9. realm: realm_id,
  10. shortname: name.into(),
  11. })?;
  12. Ok(())
  13. }
  14. pub fn list_groups(
  15. qi: &microrm::QueryInterface,
  16. realm_id: schema::RealmID,
  17. ) -> Result<(), UIDCError> {
  18. for group in qi.get().by(schema::Group::Realm, &realm_id).all()? {
  19. println!("{}", group.shortname);
  20. }
  21. Ok(())
  22. }
  23. pub fn list_members(
  24. qi: &microrm::QueryInterface,
  25. realm_id: schema::RealmID,
  26. name: &str,
  27. ) -> Result<(), UIDCError> {
  28. let group_id = qi
  29. .get()
  30. .only_ids()
  31. .by(schema::Group::Realm, &realm_id)
  32. .by(schema::Group::Shortname, name)
  33. .one_id()?
  34. .ok_or(UIDCError::Abort("no such group"))?;
  35. for member in qi
  36. .get()
  37. .by(schema::GroupMembership::Group, &group_id)
  38. .all()?
  39. {
  40. let user = qi
  41. .get()
  42. .by_id(&member.user)
  43. .one()?
  44. .ok_or(UIDCError::Abort("no user matching GroupMembership"))?;
  45. println!("{}", user.username);
  46. }
  47. Ok(())
  48. }
  49. pub fn list_roles(
  50. qi: &microrm::QueryInterface,
  51. realm_id: schema::RealmID,
  52. name: &str,
  53. ) -> Result<(), UIDCError> {
  54. let group_id = qi
  55. .get()
  56. .only_ids()
  57. .by(schema::Group::Realm, &realm_id)
  58. .by(schema::Group::Shortname, name)
  59. .one_id()?
  60. .ok_or(UIDCError::Abort("no such group"))?;
  61. for member in qi.get().by(schema::GroupRole::Group, &group_id).all()? {
  62. let role = qi
  63. .get()
  64. .by_id(&member.role)
  65. .one()?
  66. .ok_or(UIDCError::Abort("no role matching GroupRole"))?;
  67. println!("{}", role.shortname);
  68. }
  69. Ok(())
  70. }
  71. pub fn attach_user(
  72. qi: &microrm::QueryInterface,
  73. realm_id: schema::RealmID,
  74. group_name: &str,
  75. username: &str,
  76. ) -> Result<(), UIDCError> {
  77. let group = qi
  78. .get()
  79. .by(schema::Group::Realm, &realm_id)
  80. .by(schema::Group::Shortname, group_name)
  81. .one()?;
  82. let user = qi
  83. .get()
  84. .by(schema::User::Realm, &realm_id)
  85. .by(schema::User::Username, username)
  86. .one()?;
  87. match (group, user) {
  88. (None, _) => Err(UIDCError::Abort("no such group")),
  89. (_, None) => Err(UIDCError::Abort("no such user")),
  90. (Some(group), Some(user)) => {
  91. qi.add(&schema::GroupMembership {
  92. group: group.id(),
  93. user: user.id(),
  94. })?;
  95. Ok(())
  96. }
  97. }
  98. }
  99. pub fn detach_user(
  100. qi: &microrm::QueryInterface,
  101. realm_id: schema::RealmID,
  102. group_name: &str,
  103. username: &str,
  104. ) -> Result<(), UIDCError> {
  105. todo!()
  106. }
  107. pub fn attach_role(
  108. qi: &microrm::QueryInterface,
  109. realm_id: schema::RealmID,
  110. group_name: &str,
  111. role_name: &str,
  112. ) -> Result<(), UIDCError> {
  113. let group = qi
  114. .get()
  115. .by(schema::Group::Realm, &realm_id)
  116. .by(schema::Group::Shortname, group_name)
  117. .one()?;
  118. let role = qi
  119. .get()
  120. .by(schema::Role::Realm, &realm_id)
  121. .by(schema::Role::Shortname, role_name)
  122. .one()?;
  123. match (group, role) {
  124. (None, _) => Err(UIDCError::Abort("no such group")),
  125. (_, None) => Err(UIDCError::Abort("no such role")),
  126. (Some(group), Some(role)) => {
  127. qi.add(&schema::GroupRole {
  128. group: group.id(),
  129. role: role.id(),
  130. })?;
  131. Ok(())
  132. }
  133. }
  134. }
  135. pub fn detach_role(
  136. qi: &microrm::QueryInterface,
  137. realm_id: schema::RealmID,
  138. group_name: &str,
  139. role: &str,
  140. ) -> Result<(), UIDCError> {
  141. todo!()
  142. }