123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- use crate::{schema, UIDCError};
- use microrm::prelude::*;
- pub fn create_group(
- qi: µrm::QueryInterface,
- realm_id: schema::RealmID,
- name: &str,
- ) -> Result<(), UIDCError> {
- qi.add(&schema::Group {
- realm: realm_id,
- shortname: name.into(),
- })?;
- Ok(())
- }
- pub fn list_groups(
- qi: µrm::QueryInterface,
- realm_id: schema::RealmID) -> Result<(), UIDCError> {
-
- for group in qi.get().by(schema::Group::Realm, &realm_id).all()? {
- println!("{}", group.shortname);
- }
- Ok(())
- }
- pub fn list_members(
- qi: µrm::QueryInterface,
- realm_id: schema::RealmID,
- name: &str,
- ) -> Result<(), UIDCError> {
- 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"))?;
- for member in qi.get().by(schema::GroupMembership::Group, &group_id).all()? {
- let user = qi.get().by_id(&member.user).one()?.ok_or(UIDCError::Abort("no user matching GroupMembership"))?;
- println!("{}", user.username);
- }
- Ok(())
- }
- pub fn list_roles(
- qi: µrm::QueryInterface,
- realm_id: schema::RealmID,
- name: &str,
- ) -> Result<(), UIDCError> {
- 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"))?;
- for member in qi.get().by(schema::GroupRole::Group, &group_id).all()? {
- let role = qi.get().by_id(&member.role).one()?.ok_or(UIDCError::Abort("no role matching GroupRole"))?;
- println!("{}", role.shortname);
- }
- Ok(())
- }
- pub fn attach_user(
- qi: µrm::QueryInterface,
- realm_id: schema::RealmID,
- group_name: &str,
- username: &str,
- ) -> Result<(), UIDCError> {
- let group = qi
- .get()
- .by(schema::Group::Realm, &realm_id)
- .by(schema::Group::Shortname, group_name)
- .one()?;
- let user = qi
- .get()
- .by(schema::User::Realm, &realm_id)
- .by(schema::User::Username, username)
- .one()?;
- match (group, user) {
- (None, _) => Err(UIDCError::Abort("no such group")),
- (_, None) => Err(UIDCError::Abort("no such user")),
- (Some(group), Some(user)) => {
- qi.add(&schema::GroupMembership {
- group: group.id(),
- user: user.id(),
- })?;
- Ok(())
- }
- }
- }
- pub fn detach_user(
- qi: µrm::QueryInterface,
- realm_id: schema::RealmID,
- group_name: &str,
- username: &str,
- ) -> Result<(), UIDCError> {
- todo!()
- }
- pub fn attach_role(
- qi: µrm::QueryInterface,
- realm_id: schema::RealmID,
- group_name: &str,
- role_name: &str,
- ) -> Result<(), UIDCError> {
- let group = qi
- .get()
- .by(schema::Group::Realm, &realm_id)
- .by(schema::Group::Shortname, group_name)
- .one()?;
- let role = qi
- .get()
- .by(schema::Role::Realm, &realm_id)
- .by(schema::Role::Shortname, role_name)
- .one()?;
- match (group, role) {
- (None, _) => Err(UIDCError::Abort("no such group")),
- (_, None) => Err(UIDCError::Abort("no such role")),
- (Some(group), Some(role)) => {
- qi.add(&schema::GroupRole {
- group: group.id(),
- role: role.id(),
- })?;
- Ok(())
- }
- }
- }
- pub fn detach_role(
- qi: µrm::QueryInterface,
- realm_id: schema::RealmID,
- group_name: &str,
- role: &str,
- ) -> Result<(), UIDCError> {
- todo!()
- }
|