Пример #1
0
    /// <summary>
    /// Event will Save or Update the User information.
    /// </summary>
    /// <param name="sender">Sender object</param>
    /// <param name="e">Event argument</param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            bool result = false;

            GetUserInformation();
            ZentityUserAdmin adminObject = new ZentityUserAdmin(userToken);

            if (!string.IsNullOrEmpty(loginName))
            {
                currentUser = new ZentityUser(loginName, userToken, currentUserProfile);

                if (userInfoPanel.Enabled)
                {
                    result = UserManager.UpdateUser(currentUser, userToken);
                }

                if (groupAssignment.IsEnable)
                {
                    if (result)
                    {
                        result = adminObject.SetAccountStatus(currentUser.LogOnName, GetAccountStatus());
                        using (ResourceDataAccess dataAccess = new ResourceDataAccess(Utility.CreateContext()))
                        {
                            List <String> groupList     = AddGroupsInIdentity();
                            List <String> allSearchList = groupAssignment.GetSearchList();

                            Identity identity = (Identity)dataAccess.GetResource(new Guid(id));
                            identity.Groups.Load();

                            List <string> existingGroups = identity.Groups
                                                           .Select(group => group.Id.ToString())
                                                           .ToList();

                            foreach (string exsitingId in existingGroups)
                            {
                                if (!groupList.Contains(exsitingId))
                                {
                                    if (allSearchList.Contains(exsitingId))
                                    {
                                        Group group = (Group)dataAccess.GetResource(new Guid(exsitingId));
                                        dataAccess.RemoveIdentityFromGroup(identity, group, userToken);
                                    }
                                }
                            }

                            foreach (string selectedId in groupList)
                            {
                                if (!existingGroups.Contains(selectedId))
                                {
                                    Group group = (Group)dataAccess.GetResource(new Guid(selectedId));
                                    result = dataAccess.AddIdentityToGroup(group, identity, userToken);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                using (ResourceDataAccess dataAccess = new ResourceDataAccess(Utility.CreateContext()))
                {
                    result = dataAccess.CreateUser(currentUser, userToken);
                    if (result)
                    {
                        result = adminObject.SetAccountStatus(currentUser.LogOnName, GetAccountStatus());
                        Identity      identity  = UserManager.GetIdentity(currentUser.LogOnName, Utility.CreateContext());
                        List <String> groupList = AddGroupsInIdentity();
                        foreach (String groupId in groupList)
                        {
                            Group group = (Group)dataAccess.GetResource(new Guid(groupId));
                            result = dataAccess.AddIdentityToGroup(group, identity, userToken);
                        }
                    }
                }
            }

            if (!result)
            {
                if (string.IsNullOrEmpty(loginName))
                {
                    Utility.ShowMessage(lblMessage, Resources.Resources.LabelErrorRegistrationFail, true);
                }
                else
                {
                    Utility.ShowMessage(lblMessage, Resources.Resources.LabelErrorUpdateUserFail, true);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(loginName))
                {
                    Utility.ShowMessage(lblMessage,
                                        string.Format(CultureInfo.InvariantCulture, Resources.Resources.LabelUserInfoUpdated, currentUser.LogOnName),
                                        false);
                }
                else
                {
                    Utility.ShowMessage(lblMessage,
                                        string.Format(CultureInfo.InvariantCulture, Resources.Resources.LabelRegistrationCompleted, currentUser.LogOnName),
                                        false);
                    ResetRegistrationForm();
                }
                FillUserGrid();
            }
        }
        catch (Exception ex)
        {
            Utility.ShowMessage(lblMessage, ex.Message, true);
        }
    }