/// <summary> /// creates an organization for maphhive user and sets links as expected. org name, slug and such is based on user email! /// </summary> /// <param name="dbCtx"></param> /// <returns></returns> public async Task <Organization> CreateUserOrganizationAsync(DbContext dbCtx) { if (string.IsNullOrEmpty(Slug)) { throw new Exception("Cannot create a user organization without a valid slug."); } //org creation step 1 var org = new Organization { DisplayName = $"{Email}'s organization", Slug = Utils.Slug.GetOrgSlug(null, Email) //no org name here }; //tie up user to an org org.AddLink(this); await org.CreateAsync(dbCtx); //tie up the org to a user UserOrgId = org.Uuid; //add org owner role to user this.AddLink(await org.GetOrgOwnerRoleAsync(dbCtx)); await this.UpdateAsync(dbCtx); return(org); }
/// <summary> /// Gets a list of owners of an organisation /// </summary> /// <param name="org"></param> /// <param name="dbCtx"></param> /// <returns></returns> public static async Task <IEnumerable <MapHiveUser> > GetOwnersAsync(this Organization org, DbContext dbCtx) { var mhDb = (MapHiveDbContext)dbCtx; var ownerR = await org.GetOrgOwnerRoleAsync(dbCtx); return(await ownerR.GetParentsAsync <Role, MapHiveUser>(mhDb)); }
/// <summary> /// Removes owner from an organisation; this is done by simpoy removing a link to the owner role /// </summary> /// <param name="org"></param> /// <param name="dbCtx"></param> /// <param name="u"></param> /// <returns></returns> public static async Task RemoveOwnerAsync(this Organization org, DbContext dbCtx, MapHiveUser u) { //first grab the owner role for the org var ownerR = await org.GetOrgOwnerRoleAsync(dbCtx); //and remove the link var mhDb = (MapHiveDbContext)dbCtx; mhDb.Links.Remove( mhDb.Links.FirstOrDefault(r => r.ParentUuid == u.Uuid && r.ChildUuid == ownerR.Uuid)); await mhDb.SaveChangesAsync(); }
/// <summary> /// /// </summary> /// <param name="org"></param> /// <param name="dbCtx"></param> /// <param name="u"></param> /// <returns></returns> public static async Task AddOwnerAsync(this Organization org, DbContext dbCtx, MapHiveUser u) { //first find the owner role - it should always be there as it is created on org create var ownerR = await org.GetOrgOwnerRoleAsync(dbCtx); //and next link it to a user u.AddLink(ownerR); await u.UpdateWithNoIdentityChangesAsync <MapHiveUser>(dbCtx); //and also add a user to an org org.AddLink(u); await org.UpdateAsync(dbCtx); }