| 1 | //! Types and service interfaces shared by every g1t service. |
| 2 | //! |
| 3 | //! Each service has a module here holding the data it exchanges and the |
| 4 | //! arguments of each of its methods. Services and their callers depend on |
| 5 | //! this crate, never on each other's code. |
| 6 | |
| 7 | pub mod events; |
| 8 | pub mod identity; |
| 9 | mod ids; |
| 10 | mod names; |
| 11 | mod outcome; |
| 12 | pub mod repos; |
| 13 | pub mod time; |
| 14 | pub mod work; |
| 15 | |
| 16 | pub use ids::new_id; |
| 17 | pub use names::{is_valid_namespace, is_valid_repo_name}; |
| 18 | pub use outcome::{Failure, FailureCode, Outcome}; |
| 19 | |
| 20 | use serde::{Deserialize, Serialize}; |
| 21 | |
| 22 | /// What a member may do in a workspace. |
| 23 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 24 | #[serde(rename_all = "lowercase")] |
| 25 | pub enum Role { |
| 26 | /// Everything a member can, plus managing members. |
| 27 | Owner, |
| 28 | /// Create repositories, push, open intents and ship. |
| 29 | Member, |
| 30 | } |
| 31 | |
| 32 | /// One workspace a user belongs to. |
| 33 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 34 | pub struct Membership { |
| 35 | /// The workspace's name in URLs: `g1t.sh/<slug>`. |
| 36 | pub slug: String, |
| 37 | pub role: Role, |
| 38 | } |
| 39 | |
| 40 | #[derive(Clone, Debug, Default, Serialize, Deserialize)] |
| 41 | pub struct User { |
| 42 | pub id: String, |
| 43 | pub username: String, |
| 44 | /// Whether the account's email address has been confirmed. Unverified |
| 45 | /// accounts can sign in but cannot create or change anything. |
| 46 | #[serde(default)] |
| 47 | pub verified: bool, |
| 48 | /// The workspaces this user belongs to. Filled in when a user is |
| 49 | /// resolved from credentials, so any service can authorize from it. |
| 50 | #[serde(default)] |
| 51 | pub workspaces: Vec<Membership>, |
| 52 | } |
| 53 | |
| 54 | impl User { |
| 55 | pub fn role_in(&self, slug: &str) -> Option<Role> { |
| 56 | self.workspaces |
| 57 | .iter() |
| 58 | .find(|membership| membership.slug == slug) |
| 59 | .map(|membership| membership.role) |
| 60 | } |
| 61 | |
| 62 | pub fn is_member(&self, slug: &str) -> bool { |
| 63 | self.role_in(slug).is_some() |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// Who is asking. Every read and write in every service takes one. |
| 68 | pub type Viewer = Option<User>; |