如何自动将新用户提供给系统用户角色以及如何在网站顶部显示它?

时间:2012-02-17 13:07:47

标签: c# asp.net

我有一个员工表的以下数据库设计:

  • 用户名
  • 名称
  • 工作
  • 等..

角色表:

  • 角色ID
  • ROLENAME

最后,一个UserRole表:

  • UserRoleID
  • 用户名
  • 角色ID

我正在为公司的部门开发一个Intranet基于Web的应用程序。此应用程序只能由我的部门员工访问,并且应该是具有其角色(访问类型)的员工的用户名在网站顶部。我有四个不同的角色;经理,贡献,助理和用户。我现在想要做的是:

  1. 检查用户是否是部门员工之一。
  2. 如果没有,他会看到错误页面
  3. 如果是,他将能够直接访问该网站,这是他第一次访问该网站,然后他应该获得一个用户角色,除非管理员,该角色应立即显示在用户名的顶部加上他并给了他其中一个角色。
  4. 除了用户不是用户角色之外,所有内容都运行正常,除非管理员确定他在数据库中的访问权限,否则如果用户是系统新用户,则角色不会显示在顶部

    那么,我如何为新用户提供默认角色,并将其显示在除用户名之外的网站顶部?

    我的代码隐藏如下:

    private bool CheckUsername(string username)
        {
            if (Service.GetPerson(username).GetProperty("RES_NETID").Equals("-"))
                return false;
            else if (Security.isPMODMember(username))
                return true;
            else
                return false;
    
            //string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
            //string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'";
            //using (SqlConnection conn = new SqlConnection(connString))
            //{
            //    conn.Open();
            //    // Open DB connection.
            //    using (SqlCommand cmd = new SqlCommand(cmdText, conn))
            //    {
            //        int count = (int)cmd.ExecuteScalar();
            //        // True (> 0) when the username exists, false (= 0) when the username does not exist.
            //        return (count > 0);
            //    }
            //}
        }
    
    
        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            string username = TextBox1.Text;
            string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
    
            switch (Wizard1.WizardSteps[e.NextStepIndex].ID)
            {
                case "WizardStep2":
    
                    //For checking the user        
                    if (!String.IsNullOrEmpty(username) && CheckUsername(username))
                    {
                        try
                        {
                            SqlConnection conn = new SqlConnection(connString);
                            conn.Open();
                            string cmdText = @"SELECT dbo.employee.Username, dbo.employee.Name, dbo.employee.JobTitle, dbo.employee.BadgeNo,
                                                    ISNULL(dbo.Roles.RoleID, 3) AS RoleID, dbo.Divisions.DivisionName, dbo.Roles.RoleName
                                             FROM  dbo.Divisions INNER JOIN dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode
                                                    LEFT OUTER JOIN dbo.Roles RIGHT OUTER JOIN dbo.UserRole ON dbo.Roles.RoleID = dbo.UserRole.RoleID ON
                                                    dbo.employee.Username = dbo.UserRole.Username
                                             WHERE     (dbo.employee.Username = @Username)";
                            SqlCommand myCommand = new SqlCommand(cmdText, conn);
                            myCommand.Parameters.AddWithValue("@Username", username);
                            DataTable table = new DataTable();
                            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                            adapter.Fill(table);
    
                            ObjectUser user = new ObjectUser(username, true);
    
                            string Name = user.Name;
                            string Username = user.ID;
                            string DivisionName = user.Org.Title;
                            string JobTitle = user.GetProperty("EMP_TITLE");
                            string BadgeNo = user.GetProperty("EMP_BADGE_NUMBER");
                            string role = "User";
                            string roleid = "3";
                            if (table.Rows.Count > 0)
                            {
                                role = table.Rows[0]["RoleName"] as string;
                                roleid = table.Rows[0]["RoleID"].ToString();
                            }
    
                            lblName.Text = Name;
                            lblUsername.Text = Username;
                            lblDivision.Text = DivisionName;
                            lblJobTitle.Text = JobTitle;
                            lblBadgeNo.Text = BadgeNo;
    
                            lblRole.Text = role;
                            radio1.SelectedValue = roleid;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
                    }
    
                    else
                    {
                        //If the user does not exist or a blank value has been entered
                        //Cancel the nextstep redirection and display an error message in a span
                        e.Cancel = true;
                        errorSpan.InnerText = "The username specified is blank or does not belong to PMOD";
                    }
    
                    break;
                case "WizardStep3":
    
                    break;
            }
        }
    
    
    
    
        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            //If one of the items is selected AND a username exists in the Username session object update the user role
            string username = TextBox1.Text;
    
            if (!String.IsNullOrEmpty(radio1.SelectedValue) && !String.IsNullOrEmpty(username))
            {
                string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
    
                //This for adding the new PMOD user to the system
                string insertUserCommand = "INSERT INTO employee (Name, Username, JobTitle, BadgeNo, EmpOrgType, DivisionCode) values (@Name, @Username, @JobTitle, @BadgeNo, @EmpOrgType, @DivisionCode)";
                string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'";
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    // Open DB connection.
                    using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                    {
                        if ((int)cmd.ExecuteScalar() == 0)
                        {
                            //An object from ObjectUser class to get the user information from the Secure system and insert them to the database
                            ObjectUser user = new ObjectUser(username, true);
    
                            SqlCommand cmd2 = new SqlCommand(insertUserCommand, conn);
                            cmd2.Parameters.AddWithValue("@Name", user.Name);
                            cmd2.Parameters.AddWithValue("@Username", username);
                            cmd2.Parameters.AddWithValue("@JobTitle", user.GetProperty("EMP_TITLE"));
                            cmd2.Parameters.AddWithValue("@BadgeNo", user.GetProperty("EMP_BADGE_NUMBER"));
                            cmd2.Parameters.AddWithValue("@EmpOrgType", user.GetProperty("EMP_EMPTYPE"));
                            cmd2.Parameters.AddWithValue("@DivisionCode", user.Org.Division.SapCode);
                            cmd2.ExecuteNonQuery();
                        }
    
                    }
                }
    
                //For updating the role of the user by deleting its current role and inserting a new role
                string deleteCommand = "DELETE FROM UserRole where Username=@Username";
                string insertCommand = "INSERT INTO UserRole (RoleID,Username) values(@RoleID,@Username)";
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    //using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                    using (SqlCommand cmd = new SqlCommand(deleteCommand, conn))
                    {
                        cmd.Parameters.AddWithValue("@Username", username);
                        cmd.ExecuteNonQuery();
                        //Now the insert
                        cmd.CommandText = insertCommand;
                        cmd.Parameters.Clear(); //need this because still has params from del comm
                        cmd.Parameters.AddWithValue("@RoleID", radio1.SelectedValue);
                        cmd.Parameters.AddWithValue("@Username", username);
                        cmd.ExecuteNonQuery();
                        //infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
                        //cmd.ExecuteScalar();
                        //infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
                    }
                }
    
                Wizard1.Visible = false;
                wizard.InnerHtml = @"<p><b>The task has been done successfully.</b> <br /> <a href='UserManagement.aspx'>Edit Another User</a></p>";
            }
    
    
        }
    

    我想我已经在如上所示的向导步骤#2中做到了,但现在它确实有效,我不知道为什么。有什么帮助吗?

0 个答案:

没有答案