/// <summary> /// /// </summary> /// <param name="ds"></param> /// <returns></returns> public ModelUser ToModel(int?orgId) { var model = new ModelUser() { Id = Id, OrgId = OrgId, Login = Login, Name = Name, IsRoot = IsRoot, Email = Email, LastSeenAt = LastSeenAt }; if (orgId.HasValue) { model.OrgId = orgId.Value; } model.Preferences = new UserPreferences() { Theme = Preferences?.Theme ?? Theme.Default, TimeZone = Preferences?.TimeZone ?? Security.TimeZone.Default, HomeDashboardId = Preferences?.HomeDashboardId, }; var member = OrgMember.FirstOrDefault(x => x.OrgId == model.OrgId); if (null != member) { model.Role = member.Role; model.Bag.OrgName = member.Org.Name; } return(model); }
/// <summary> /// /// </summary> /// <param name="teamId"></param> /// <param name="user"></param> /// <returns></returns> public OperationResult <ModelUser> AddOrgMember(int orgId, int userId, Role role) { OperationResult <ModelUser> res; ModelUser affectedUser = null; try { var user = DataContext .Users .Include(x => x.OrgMember) .ThenInclude(x => x.Org) .FirstOrDefault(x => x.Id == userId); if (null == user) { return(OperationResult <ModelUser> .Create(ErrorCode.BadGetUser)); } if (0 == user.OrgId) { var org = DataContext .Orgs .FirstOrDefault(x => x.Id == orgId); user.OrgId = orgId; affectedUser = user .ToModel(orgId) .AddVersion(DataContext.Entry(user)); affectedUser.Role = role; affectedUser.Bag.OrgName = org.Name; } DataContext.Add(new OrgMember() { OrgId = orgId, UserId = userId, Role = role }); var count = DataContext.SaveChanges(); res = OperationResult <ModelUser> .Create(affectedUser); } catch (Exception e) { res = OperationResult <ModelUser> .Create(ErrorCode.BadAddOrgMember, e); } return(res); }
/// <summary> /// /// </summary> public static ModelUser AddPreferences(this ModelUser u, DataContext dc) { var res = new UserRepository(dc) .ForActiveOrg(u.OrgId) .GetEffectivePreferences(u.Id); if (!res.HasError) { u.Preferences = res.Value; } return(u); }
/// <summary> /// /// </summary> /// <param name="team"></param> /// <returns></returns> public static EntityUser ToEntity(this ModelUser user) { var entity = new EntityUser() { Id = user.Id, OrgId = user.OrgId, Login = user.Login, Name = user.Name, IsRoot = user.IsRoot, Email = user.Email, LastSeenAt = user.LastSeenAt, }; entity.ChangePassword(user.Password ?? string.Empty); return(entity); }
/// <summary> /// /// </summary> /// <param name="team"></param> /// <returns></returns> public static ModelUser SubstituteOrg(this ModelUser user, int orgId) { user.OrgId = orgId; return(user); }
/// <summary> /// /// </summary> /// <param name="user"></param> public void Update(ModelUser user) { Login = user.Login; Name = user.Name; Email = user.Email; }
/// <summary> /// /// </summary> /// <param name="orgId"></param> /// <param name="userId"></param> /// <returns></returns> public OperationResult <ModelUser> RemoveMember(int orgId, int userId) { OperationResult <ModelUser> res; ModelUser affectedUser = null; try { var org = DataContext .Orgs .Include(x => x.OrgMember) .ThenInclude(x => x.User) .ThenInclude(x => x.OrgMember) .ThenInclude(x => x.Org) .First(x => x.Id == orgId); var om = org .OrgMember .Where(x => x.UserId == userId) .FirstOrDefault(); var user = om.User; var isLastActorMembership = (userId == DataContext.ActiveUserId) && (user.OrgMember.Count == 1) && (user.OrgMember [0].OrgId == orgId); if (isLastActorMembership) { return(OperationResult <ModelUser> .Create(ErrorCode.BadDeleteOrgMember)); } org.OrgMember.Remove(om); // if user's current org membership was deleted if (user.OrgId == orgId) { var nextOrgCandidate = om .User .OrgMember .FirstOrDefault(x => x.OrgId != orgId); user.OrgId = (null == nextOrgCandidate) ? 0 : nextOrgCandidate.OrgId; affectedUser = user .ToModel() .AddVersion(DataContext.Entry(user)); } var count = DataContext.SaveChanges(); res = OperationResult <ModelUser> .Create( () => count > 0, affectedUser, ErrorCode.BadDeleteOrgMember); } catch (Exception e) { res = OperationResult <ModelUser> .Create(ErrorCode.BadDeleteOrgMember, e); } return(res); }