Esempio n. 1
0
        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            // Name cannot be empty
            if (!FormHelper.ValidateNotEmpty(txtName, Resources.MsgNameRequired))
            {
                return;
            }

            if (this.Group == null)
            {
                // Name cannot be the built-in administrator group name
                if (GuiHelper.AdministratorGroupName.Equals(txtName.Text, StringComparison.OrdinalIgnoreCase))
                {
                    FormHelper.ShowError(Resources.MsgAdminGroupNameNotAllowed);
                    txtName.Focus();
                    return;
                }
            }

            using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    try
                    {
                        if (Group == null)
                        {
                            // Name must be unique
                            Role searchRole = Role.SingleOrDefault(r => r.Name.ToLower() == txtName.Text.ToLower());
                            if (searchRole != null)
                            {
                                FormHelper.ShowError(Resources.MsgGroupNameNotUnique);
                                return;
                            }

                            // Create a new one
                            Role role = new Role();
                            role.Name = txtName.Text.Trim();
                            role.Desc = txtDescription.Text;
                            if (role.Name.Equals(GuiHelper.AdministratorGroupName))
                            {
                                role.CanBeDeleted = false;
                            }
                            else
                            {
                                role.CanBeDeleted = true;
                            }
                            role.Save();
                            this.Group = role;
                        }
                        else
                        {
                            if (!this.Group.Name.Equals(txtName.Text.Trim(), StringComparison.OrdinalIgnoreCase))
                            {
                                // Check for duplicate
                                Role searchRole = Role.SingleOrDefault(r => r.Name.ToLower() == txtName.Text.ToLower());
                                if (searchRole != null)
                                {
                                    FormHelper.ShowError(Resources.MsgGroupNameNotUnique);
                                    return;
                                }
                            }

                            // Update an existing one
                            this.Group.Name = txtName.Text.Trim();
                            this.Group.Desc = txtName.Text;
                            if (this.Group.Name.Equals(GuiHelper.AdministratorGroupName))
                            {
                                this.Group.CanBeDeleted = false;
                            }
                            else
                            {
                                this.Group.CanBeDeleted = true;
                            }
                            this.Group.Update();
                        }


                        // Delete user related to existing groups
                        UserRoleMap.Delete(urm => urm.RoleId == this.Group.Id);

                        // Save the users for the existing groups
                        foreach (object item in lstUsersInGroup.Items)
                        {
                            string loginName = GetLoginName(item);

                            // Get the user id and save the user group mapping
                            User user = User.SingleOrDefault(u => u.LoginName == loginName);
                            if (user != null)
                            {
                                UserRoleMap urm = new UserRoleMap();
                                urm.UserId = user.Id;
                                urm.RoleId = this.Group.Id;
                                urm.Save();
                            }
                        }

                        FormHelper.ShowInfo(Resources.MsgGroupSaved);
                    }
                    catch (Exception ex)
                    {
                        FormHelper.ShowError(ex.Message);
                    }
                    finally
                    {
                        try
                        {
                            ts.Complete();
                        }
                        catch (Exception) { }
                    }
                }
            }

            this.DialogResult = DialogResult.OK;
        }
        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            // Name cannot be empty
            if (!FormHelper.ValidateNotEmpty(txtName, Resources.MsgNameRequired))
            {
                return;
            }

            // Login name cannot be empty
            if (!FormHelper.ValidateNotEmpty(txtLoginName, Resources.MsgLoginNameRequired))
            {
                return;
            }

            // Password cannot be empty
            if (!FormHelper.ValidateNotEmpty(txtPassword, Resources.MsgPasswordRequired))
            {
                return;
            }

            if (this.User == null)
            {
                // Login name cannot be the built-in administrator name
                if (GuiHelper.AdministratorName.Equals(txtLoginName.Text, StringComparison.OrdinalIgnoreCase))
                {
                    FormHelper.ShowError(Resources.MsgAdminUserNameNotAllowed);
                    txtLoginName.Focus();
                    return;
                }
            }

            // Validate passwords are the same
            if (!txtPassword.Text.Equals(txtPasswordAgain.Text))
            {
                FormHelper.ShowError(Resources.MsgPasswordNotMatched);
                txtPassword.Focus();
                return;
            }

            using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    try
                    {
                        if (User == null)
                        {
                            // Login name must be unique
                            if (IsDuplicate())
                            {
                                return;
                            }

                            // Create a new one
                            User user = new User();
                            user.CommonName = txtName.Text;
                            user.Mobtel     = txtPhoneNumber.Text;
                            user.Email      = txtEmail.Text;
                            user.LoginName  = txtLoginName.Text;
                            user.Password   = txtPassword.Text;
                            if (user.LoginName.Equals(GuiHelper.AdministratorName))
                            {
                                user.CanBeDeleted = false;
                            }
                            else
                            {
                                user.CanBeDeleted = true;
                            }
                            user.Save();

                            this.User = user;
                        }
                        else
                        {
                            if (!this.User.LoginName.Equals(txtLoginName.Text.Trim(), StringComparison.OrdinalIgnoreCase))
                            {
                                if (IsDuplicate())
                                {
                                    return;
                                }
                            }

                            // Update an existing one
                            this.User.CommonName = txtName.Text;
                            this.User.Mobtel     = txtPhoneNumber.Text;
                            this.User.Email      = txtEmail.Text;
                            this.User.LoginName  = txtLoginName.Text;
                            this.User.Password   = txtPassword.Text;
                            if (this.User.LoginName.Equals(GuiHelper.AdministratorName))
                            {
                                this.User.CanBeDeleted = false;
                            }
                            else
                            {
                                this.User.CanBeDeleted = true;
                            }
                            this.User.Update();
                        }


                        // Delete groups for this users
                        UserRoleMap.Delete(urm => urm.UserId == this.User.Id);

                        // Save the groups for this user
                        foreach (object item in lstBelongedGroups.Items)
                        {
                            string groupName = item as string;

                            // Get the user id and save the user role mapping
                            Role role = Role.SingleOrDefault(r => r.Name == groupName);
                            if (role != null)
                            {
                                UserRoleMap urm = new UserRoleMap();
                                urm.UserId = this.User.Id;
                                urm.RoleId = role.Id;
                                urm.Save();
                            }
                        }

                        FormHelper.ShowInfo(Resources.MsgUserSaved);
                    }
                    catch (Exception ex)
                    {
                        FormHelper.ShowError(ex.Message);
                    }
                    finally
                    {
                        try
                        {
                            ts.Complete();
                        }
                        catch (Exception) { }
                    }
                }
            }

            this.DialogResult = DialogResult.OK;
        }