如何根据数据库权限设置代码限制

时间:2016-03-10 14:30:20

标签: c# sql-server visual-studio

我在Visual Studio中制作拍卖应用程序(WPF),需要与在SQL Server 2014中创建的数据库连接。此应用程序是作业的一部分,因此我必须遵循一些规则。

我需要帮助的是关于根据角色设置限制。我是数据库,我有一个列isAdmin的表(如果用户是admin,则为1,否则为0)。在Visual Studio中,我需要创建一个UI,用户可以在访问主要拍卖窗口之前登录。在数据库中,我还有用户列表(每个用户都有一个用于登录的用户名和密码以及isAdmin的值)。

当他们以管理员身份登录时,他们可以访问按钮控件,以便在竞价中添加或删除商品;以用户身份登录时,他们只能出价;客人只能查看拍卖。

我需要帮助如何编写代码,以便用户可以登录并基于他们的isAdmin值并具有对主拍卖窗口中按钮的相应访问权限。

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。我将使用一个查询来做它,所以它可能看起来有点奇怪......

我们将有一个能够管理所有事情的功能......

public bool login(string user, string password, ref string isAdmin) { }

这将是我将使用的功能。在函数内部,我们现在将访问数据库并检查以确保此人输入了正确的凭据。我假设你已经设置了你的SQL连接(我将调用myConnection ...如果你需要帮助,只需要评论这样说)。

public bool login(string user, string password, ref string isAdmin){        

    //This is used so we know if we entered the while loop...
    bool success = false;

    //This is what I am going to use to execute commands
    myCommand = new SqlCommand("SELECT isAdmin FROM users WHERE user = @user AND pass = @pass", myConnection);
    myCommand.Parameters.Addwithvalue("@user", user);
    myCommand.Parameters.Addwithvalue("@pass", password); 

    try
    {
        //Open connection
        myConnection.Open();

        //Execute the reader
        myReader = myCommand.ExecuteReader();

        //Read the data now
        while(myReader.Read())
        {
            //isAdmin could very well be an int and myReader could convert to int instead of string
            isAdmin = myReader[0].ToString();   

            success = true;
        }

        //Close connection
        myConnection.Close();
    }
    catch
    {
        //Error checking
        myConnection.Close();
        return false;
    }

    if(success)
    {
        //Login worked - credentials were right
        return true;
    }
    //Login failed - credentials were wrong
    return false;
}

现在您拥有了所需的所有信息。如果返回值为true,那么您知道用户已登录,并且您具有函数中ref字符串的isAdmin值。从这里很容易。相应地禁用或启用按钮。

编辑1

这是为了回答你的评论 -

这非常有帮助!但是,我需要更多的权限帮助。现在说"用户X"被记录(例如没有管理员权限)并查看拍卖主窗口。在这种情况下,我很难插入Panel Control并将可见性设置为false。如何确定当前的用户权限?我也很难在用户表中添加ID键,因此它简化了引用。您是否需要编写函数来确定当前用户权限,还是可以使用简单的IF语句来设置Panel可见性? - Tomislav Bijelic 13小时前

是的,您可以使用IF语句。该函数会在行isAdmin = myReader[0].ToString();上为您提供1或0(管理员或非管理员)。由于isAdmin是ref,它将自动保存到函数调用中使用的任何内容中。如果你想以另一种形式访问它,你可以在声明一个新的"表格时传递变量。打开它。我的意思是一个例子......

///////////////AUCTION MAIN WINDOW START///////////////////////
//Global variable
string strIsAdmin;

public frmAuctionMainWindow(string isAdmin)
    {
        InitializeComponent();

        strIsAdmin = isAdmin;
    }
//strIsAdmin tells you whther or not they are admin so on form load just make them visible or not visible accordingly...

private frmAuctionMainWindow_Load(object sender, EventArgs e)
{
    if(strIsAdmin == "1")
    {
        //User is admin
    }     
    else if(strIsAdmin == "0")
    {
        //User is not admin...(could use else statement if only logged users are visiting this form
    }
}


///////////////AUCTION MAIN WINDOW END///////////////////////


///////////////LOGIN FORM START//////////////////////////////

    //Button could only be visible to people who have access (logged in users)
private void btnOpenMain(object sender, EventArgs e)
{
    frmAuctionMainWindow main = new frmAuctionMainWindow(isAdmin);
    main.Show(); //Or main.ShowDialog(); depending on what you want

}

///////////////LOGIN FORM END////////////////////////////////

如果您希望每个人都能够访问主要拍卖窗口,那么您将需要发送另一个变量来告诉表单用户是否已登录。对于guest虚拟机的一个简单修复是有一个单独的表单来查看仅限一个表单,仅供登录用户使用。

修改2

还有其他用户说你应该在后端进行数据库访问。您可以创建一个使用DB执行所有操作的类...读/写等...