C # simple movie record management system: Development 7 [User Classification]

Source: Internet
Author: User

Overview

In the vast majority of data management systems, administrators and common users are usually essential, and of course there are more detailed categories. in the first few issues, we used only common users. in this phase, we will set two types of users in the system: Administrator and normal user NormalUser.

Here, we classify users to differentiate the functional permissions of different users. Generally, Administrators have the permission to use all functions, while ordinary users can only use some of the functions restricted (for example, they can only view data, without modifying the data ). of course, the administrator can grant more function permissions to common users, which will be further described later.

 

Principle

The principle is very simple. I added a global variable UserType in FrmLogin in the previous user logon interface to get the user type of the current login user, assign a logon user type (the login type comes from the user type stored in the database) to the Administrator or NormalUser in the code to determine the logon success. After obtaining the login user type, we need to set the function usage permissions of common users, obtain the global variable UserType in the forms that need to block the corresponding function buttons, and then determine whether it is NormalUser; If yes, set the Enabled attribute of the corresponding function button to false.

 

First, we need to make a small adjustment to the database. In the original database table MovieAccount and Log, a column of user type UserType is added respectively.

 

 

After the database is modified, we start to write code.

First, add a global variable UserType In the login form class FrmLogin. cs to get the user type of the current login user.

// Define a logon global variable to get "Logon" or "exit" public static string Situation; // --------------------- Add code ---------------------- // define a global variable UserType to obtain the public static string UserType of the logon user; // ------------------------- Add code ----------------------//

 

Then, modify the preceding SQL statement and add a UserType field.

// Comment out the previous select statement string SQL = "select MUserPwd from MovieAccount where MUserName = '" + txtName. text + "'"; // ----------------------- Add code ---------------------- // here, the content of getting UserType from the database is added: string SQL = "select MUserPwd, userType from MovieAccount where MUserName = '"+ txtName. text + "'"; // --------------------- Add code ----------------------//

 

Use SqlDataReader sdr to read the user type from the database and pass it to the string uType.

// Grant the first field under the user name, even if the password (MUserPwd is the first field of select) to the string pwd, and read all the passwords in turn. // Trim () the method is to remove the blank string pwd = sdr before and after the string. getString (0 ). trim (); // ----------------------- new code ----------------------------- // The Reader sdr obtains two columns of data. The 1st column is the password column. The 2nd column is the user type string uType = sdr. getString (1); // --------------------- Add code -------------------------------//

 

After successful logon, assign the logon type obtained by the string uType to the global variable UserType.

// Add the code to obtain the current logon status // obtain the current user logon status Situation = "Logon "; // --------------------- Add code ------------------------------- // assign the user type successfully logged on to the global variable UserType // obtain the UserType = uType of the currently logged on user; // --------------------- Add code -------------------------------//

 

In this way, after a user logs on to the system, the system will be able to obtain the user type of the logon system. We will perform a small test on the main form FrmMain.

Now, a label box (name: lblSayHi) is added to the main form FrmMain interface to obtain the user type for logon.

 

Then, add the following code under the loading form.

// When the form is loaded, the private void FrmMain_Load (object sender, EventArgs e) {// ------------------------------ Add code example /// here a user type is added to determine FrmLogin. userType // assign the current logon user name and logon time to the text attribute of the label // It is displayed in the label box lblCurrentUser when loading the form of the current main interface. text = "the current login user is:" + FrmLogin. uid + "User Type" + FrmLogin. userType + "Logon Time:" + FrmLogin. time; // ------------------------------ Add code -------------------- -------------- // -------------------------------- Add code -------------------------------- // send a greeting to the current user // obtain the time on the current user's computer and determine the type of the user to log on to different types of users greeting // define the integer variable intTime to get the specific hours on the user's computer and then judge int intTime = FrmLogin as follows. time. hour; // obtain the UserType global variable of the FrmLogin form, which is used to greet users of different types. string uType = FrmLogin. userType; // if (intTime> = 0 & intTime <6) {if (uType = "Administrator") lblSayHi at AM. text = "Dear" + FrmLogin. Uid + "late at night, it's time to rest! "; Else if (uType =" NormalUser ") lblSayHi. Text =" dear "+ FrmLogin. Uid +" it's time to take a break! ";}// Am-Am else if (intTime >=6 & intTime <12) {if (uType =" Administrator ") lblSayHi. text = "dear" + FrmLogin. uid + "good morning! "; Else if (uType =" NormalUser ") lblSayHi. Text =" dear "+ FrmLogin. Uid +" good morning! ";}// At six o'clock P.M.-noon, else if (intTime >=12 & intTime <18) {if (uType =" Administrator ") lblSayHi. text = "dear" + FrmLogin. uid + "Good afternoon! "; Else if (uType =" NormalUser ") lblSayHi. Text =" dear "+ FrmLogin. Uid +" Good afternoon! ";}// Else if (intTime >=18 & intTime <24) {if (uType =" Administrator ") lblSayHi. text = "dear" + FrmLogin. uid + "Good evening! "; Else if (uType =" NormalUser ") lblSayHi. Text =" dear "+ FrmLogin. Uid +" Good evening! ";} Else // otherwise, the default value is lblSayHi. text = "Welcome to movie record management system" + FrmLogin. uid; // ------------------------------ Add code example // -------------------------------- Add code example // determine the user type and set the function permission for the user if (uType = "NormalUser ") {tsbRegistration. enabled = false; tsbLog. enabled = false;} // ------------------------------ Add code ----------------------------------//}

 

Before the test, you need to manually insert several user types in the UserType column of the database MovieAccount table. Here, the admin1 user type is Administrator.

Administrator admin1 Login

 

 

Common User user1 Logon

 

 
After the user type is successfully obtained, we need to assign corresponding function permissions to the Administrator and common users (the Administrator has the permission to use all functions by default, and some functions of common users cannot be used ).

First, we disable several function buttons for common users on the main form FrmMain (set the Enabled attribute of the button to false ).

The user registration and log view buttons are disabled here (Add the following code to the loading form ).

// ------------------------------ Add code ---------------------------------- // determine the user type and set the function permission for the user if (uType = "NormalUser") {tsbRegistration. enabled = false; tsbLog. enabled = false;} // ------------------------------ Add code ----------------------------------//

 

Let's test again and use the user type NormalUser user1 to log on to the system.

 

After logging on to user1, user registration and log query become unavailable.

 

Then, on the management interface, the common user sets the permission to view data but not add, delete, and modify data.

// When the system loads the data, the private void FrmManager_Load (object sender, EventArgs e) of the DataGridView dview is automatically displayed. {// call the Refresh () method to Refresh the data; cmbforTypeSelecting. text = "global search"; cmbforTypeSelecting. items. add ("global search"); cmbforTypeSelecting. items. add ("Number"); cmbforTypeSelecting. items. add ("movie name"); cmbforTypeSelecting. items. add (""); cmbforTypeSelecting. items. add ("release time"); // ---------------------- added code ------------------- // Add the user type judgment to set the permission to use the function button if (FrmLogin. userType = "NormalUser") {btnDelete. enabled = false; btnAdd. enabled = false; btnSave. enabled = false;} // ---------------------- Add code -------------------//}

 

Let's test again. The common user user1. however, the three buttons for adding, deleting, and storing records become grayed out (unavailable.

 

Of course, a column of UserType is added, and the previous user registration function (no classified users) is available, so we have to make some modifications.

We add two radiobutton codes in the User Registration Form FrmRegistration to Perform User Classification registration during registration.

One is Administrator name: rdoAdministrator and the other is common user name: rdoNormalUser

 

Then, modify the previous user registration code. It is very simple, mainly because a uType type is added to the radiobutton CheckedChanged event to get the current user type.

Then, a column of UserType is added to the previous SQL insert statement.

// ---------------------- Add code ------------------------- // a new uType to obtain the user type value string uType = ""; if (rdoAdministrator. checked) // when the Administrator's radiobutton is clicked, uType = "Administrator"; // send it to uType, an Administrator else if (rdoNormalUser. checked) // Similarly, uType = "NormalUser"; else // if you do not click it, uType = "NormalUser" is registered by default "; // The SQL insert statement adds a column named UserTypestring sqlInsert = "insert into MovieAccount (MUserName, MUserPwd, UserType) values (@ MUserName, @ MUserPwd, @ UserType )"; // use an SQL parameter array to load the data SqlParameter [] param = {new SqlParameter ("@ MUserName", txtUid. text), new SqlParameter ("@ MUserPwd", txtPwd. text), new SqlParameter ("@ UserType", uType) // added an SQL parameter uType}; // ---------------------- added code -------------------------//

 

Now, let's test the New User Classification registration.

Register an administrator user admin03 first

Then, the database has a row of Data admin03, and the user type is Administrator.

 

Register another common user user03.

 

The database has an additional user03 user whose type is NormalUser.

 

In this way, we can complete user classification and simple function permission settings. If you are interested, you can add a column in log query-user type, which will not be discussed here.

 

Add source code

MovieRecordManagementSystem07.zip

Related Recommendations [click here to view the Directory]
  • C # simple movie record management system: developer 1 [user logon]
  • C # simple movie record management system: Development 2 [add, delete, modify, and query]
  • C # simple movie record management system: Development 3 [Password modification]
  • C # simple movie record management system: Development 4 [log view]
  • C # simple movie record management system: developer 5 [User Registration]
  • C # simple movie record management system: Development 6 [data search]
  • C # simple movie record management system: Development 8 [data backup]
  • C # simple movie record management system: Development 9 [data recovery]
  • C # simple movie record management system: Conclusion [permission Assignment]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.