group_management.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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) -> Result<(), UIDCError> {
  17. for group in qi.get().by(schema::Group::Realm, &realm_id).all()? {
  18. println!("{}", group.shortname);
  19. }
  20. Ok(())
  21. }
  22. pub fn list_members(
  23. qi: &microrm::QueryInterface,
  24. realm_id: schema::RealmID,
  25. name: &str,
  26. ) -> Result<(), UIDCError> {
  27. let group_id = qi.get().only_ids().by(schema::Group::Realm, &realm_id).by(schema::Group::Shortname, name).one_id()?.ok_or(UIDCError::Abort("no such group"))?;
  28. for member in qi.get().by(schema::GroupMembership::Group, &group_id).all()? {
  29. let user = qi.get().by_id(&member.user).one()?.ok_or(UIDCError::Abort("no user matching GroupMembership"))?;
  30. println!("{}", user.username);
  31. }
  32. Ok(())
  33. }
  34. pub fn list_roles(
  35. qi: &microrm::QueryInterface,
  36. realm_id: schema::RealmID,
  37. name: &str,
  38. ) -> Result<(), UIDCError> {
  39. let group_id = qi.get().only_ids().by(schema::Group::Realm, &realm_id).by(schema::Group::Shortname, name).one_id()?.ok_or(UIDCError::Abort("no such group"))?;
  40. for member in qi.get().by(schema::GroupRole::Group, &group_id).all()? {
  41. let role = qi.get().by_id(&member.role).one()?.ok_or(UIDCError::Abort("no role matching GroupRole"))?;
  42. println!("{}", role.shortname);
  43. }
  44. Ok(())
  45. }
  46. pub fn attach_user(
  47. qi: &microrm::QueryInterface,
  48. realm_id: schema::RealmID,
  49. group_name: &str,
  50. username: &str,
  51. ) -> Result<(), UIDCError> {
  52. let group = qi
  53. .get()
  54. .by(schema::Group::Realm, &realm_id)
  55. .by(schema::Group::Shortname, group_name)
  56. .one()?;
  57. let user = qi
  58. .get()
  59. .by(schema::User::Realm, &realm_id)
  60. .by(schema::User::Username, username)
  61. .one()?;
  62. match (group, user) {
  63. (None, _) => Err(UIDCError::Abort("no such group")),
  64. (_, None) => Err(UIDCError::Abort("no such user")),
  65. (Some(group), Some(user)) => {
  66. qi.add(&schema::GroupMembership {
  67. group: group.id(),
  68. user: user.id(),
  69. })?;
  70. Ok(())
  71. }
  72. }
  73. }
  74. pub fn detach_user(
  75. qi: &microrm::QueryInterface,
  76. realm_id: schema::RealmID,
  77. group_name: &str,
  78. username: &str,
  79. ) -> Result<(), UIDCError> {
  80. todo!()
  81. }
  82. pub fn attach_role(
  83. qi: &microrm::QueryInterface,
  84. realm_id: schema::RealmID,
  85. group_name: &str,
  86. role_name: &str,
  87. ) -> Result<(), UIDCError> {
  88. let group = qi
  89. .get()
  90. .by(schema::Group::Realm, &realm_id)
  91. .by(schema::Group::Shortname, group_name)
  92. .one()?;
  93. let role = qi
  94. .get()
  95. .by(schema::Role::Realm, &realm_id)
  96. .by(schema::Role::Shortname, role_name)
  97. .one()?;
  98. match (group, role) {
  99. (None, _) => Err(UIDCError::Abort("no such group")),
  100. (_, None) => Err(UIDCError::Abort("no such role")),
  101. (Some(group), Some(role)) => {
  102. qi.add(&schema::GroupRole {
  103. group: group.id(),
  104. role: role.id(),
  105. })?;
  106. Ok(())
  107. }
  108. }
  109. }
  110. pub fn detach_role(
  111. qi: &microrm::QueryInterface,
  112. realm_id: schema::RealmID,
  113. group_name: &str,
  114. role: &str,
  115. ) -> Result<(), UIDCError> {
  116. todo!()
  117. }