public void CopyUser() { try { int shareCount = 0; string hash = UserProxy.Password; if (UserProxy.IsPasswordChanged) { hash = Utils.HashPassword(UserProxy.Password); } UserProfile user = new UserProfile(Guid.Empty, GetUniqueName(UserProxy.Name), UserProxy.ProfileType, hash, DateTime.Now, UserProxy.Image); user.AllowedAge = UserProxy.AllowedAge; foreach (var shareId in UserProxy.SelectedShares) { user.AddAdditionalData(UserDataKeysKnown.KEY_ALLOWED_SHARE, ++shareCount, shareId.ToString()); } user.RestrictAges = UserProxy.RestrictAges; user.RestrictShares = UserProxy.RestrictShares; user.IncludeParentGuidedContent = UserProxy.IncludeParentGuidedContent; user.IncludeUnratedContent = UserProxy.IncludeUnratedContent; user.EnableRestrictionGroups = UserProxy.EnableRestrictionGroups; user.RestrictionGroups = UserProxy.RestrictionGroups; SetUser(user); // Auto save to avoid unsaved user profiles SaveUser().TryWait(); UpdateUserLists_NoLock(false, UserProxy.Id).TryWait(); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Error("UserConfigModel: Problems adding user", e); } }
public async Task SaveUser() { try { if (UserProxy.IsUserValid) { int shareCount = 0; bool success = true; string hash = UserProxy.Password; bool wasCreated = false; if (UserProxy.IsPasswordChanged) { hash = Utils.HashPassword(UserProxy.Password); } if (UserProxy.ProfileType == UserProfileType.ClientProfile) { hash = ""; //Client profiles can't have passwords } IUserManagement userManagement = ServiceRegistration.Get <IUserManagement>(); var userId = UserProxy.Id; if (userManagement.UserProfileDataManagement != null) { if (userId == Guid.Empty) { userId = UserProxy.Id = await userManagement.UserProfileDataManagement.CreateProfileAsync(UserProxy.Name, UserProxy.ProfileType, hash); wasCreated = true; } else { success = await userManagement.UserProfileDataManagement.UpdateProfileAsync(userId, UserProxy.Name, UserProxy.ProfileType, hash); } if (userId == Guid.Empty) { ServiceRegistration.Get <ILogger>().Error("UserConfigModel: Problems saving user '{0}'", UserProxy.Name); return; } if (UserProxy.Image != null) { success &= await userManagement.UserProfileDataManagement.SetProfileImageAsync(userId, UserProxy.Image); } // If the current user is restricted to own profile, we skip all properties that would allow a "self unrestriction" if (!IsRestrictedToOwn) { success &= await userManagement.UserProfileDataManagement.SetUserAdditionalDataAsync(userId, UserDataKeysKnown.KEY_ALLOWED_AGE, UserProxy.AllowedAge.ToString()); success &= await userManagement.UserProfileDataManagement.ClearUserAdditionalDataKeyAsync(userId, UserDataKeysKnown.KEY_ALLOWED_SHARE); foreach (var shareId in UserProxy.SelectedShares) { success &= await userManagement.UserProfileDataManagement.SetUserAdditionalDataAsync(userId, UserDataKeysKnown.KEY_ALLOWED_SHARE, shareId.ToString(), ++shareCount); } success &= await userManagement.UserProfileDataManagement.SetUserAdditionalDataAsync(userId, UserDataKeysKnown.KEY_ALLOW_ALL_AGES, UserProxy.RestrictAges? "0" : "1"); success &= await userManagement.UserProfileDataManagement.SetUserAdditionalDataAsync(userId, UserDataKeysKnown.KEY_ALLOW_ALL_SHARES, UserProxy.RestrictShares? "0" : "1"); success &= await userManagement.UserProfileDataManagement.SetUserAdditionalDataAsync(userId, UserDataKeysKnown.KEY_INCLUDE_PARENT_GUIDED_CONTENT, UserProxy.IncludeParentGuidedContent? "1" : "0"); success &= await userManagement.UserProfileDataManagement.SetUserAdditionalDataAsync(userId, UserDataKeysKnown.KEY_INCLUDE_UNRATED_CONTENT, UserProxy.IncludeUnratedContent? "1" : "0"); success &= await userManagement.UserProfileDataManagement.SetUserAdditionalDataAsync(userId, UserDataKeysKnown.KEY_ENABLE_RESTRICTION_GROUPS, UserProxy.EnableRestrictionGroups? "1" : "0"); success &= await userManagement.UserProfileDataManagement.SetUserAdditionalDataAsync(userId, UserDataKeysKnown.KEY_TEMPLATE_ID, UserProxy.TemplateId.ToString()); success &= await userManagement.UserProfileDataManagement.ClearUserAdditionalDataKeyAsync(userId, UserDataKeysKnown.KEY_RESTRICTION_GROUPS); int groupCount = 0; foreach (var group in UserProxy.RestrictionGroups) { success &= await userManagement.UserProfileDataManagement.SetUserAdditionalDataAsync(userId, UserDataKeysKnown.KEY_RESTRICTION_GROUPS, group, ++groupCount); } } if (!success) { ServiceRegistration.Get <ILogger>().Error("UserConfigModel: Problems saving setup for user '{0}'", UserProxy.Name); return; } } ListItem item = _userList.FirstOrDefault(i => i.Selected); if (item == null) { return; } shareCount = 0; UserProfile user = new UserProfile(userId, UserProxy.Name, UserProxy.ProfileType, hash, UserProxy.LastLogin, UserProxy.Image); if (wasCreated) { user.LastLogin = DateTime.Now; } user.RestrictAges = UserProxy.RestrictAges; user.AllowedAge = UserProxy.AllowedAge; user.RestrictShares = UserProxy.RestrictShares; foreach (var shareId in UserProxy.SelectedShares) { user.AddAdditionalData(UserDataKeysKnown.KEY_ALLOWED_SHARE, ++shareCount, shareId.ToString()); } user.IncludeParentGuidedContent = UserProxy.IncludeParentGuidedContent; user.IncludeUnratedContent = UserProxy.IncludeUnratedContent; user.EnableRestrictionGroups = UserProxy.EnableRestrictionGroups; user.RestrictionGroups = UserProxy.RestrictionGroups; user.TemplateId = UserProxy.TemplateId; // Update current logged in user if the same if (userManagement.CurrentUser.ProfileId == user.ProfileId) { userManagement.CurrentUser = user; } item.SetLabel(Consts.KEY_NAME, user.Name); item.AdditionalProperties[Consts.KEY_USER] = user; _userList.FireChange(); SetUser(user); } } catch (NotConnectedException) { DisconnectedError(); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Error("UserConfigModel: Problems saving user", e); } }