Start-to-Finish Visual C# 2015 by Tim Patrick

Start-to-Finish Visual C# 2015 by Tim Patrick

Author:Tim Patrick
Language: eng
Format: epub
Publisher: Owani Press
Published: 2016-09-01T07:00:00+00:00


Figure 11-2. The official Library Project administrative login form

I’ve already added the ChangeUser.cs form to the project. If you’re using the Before version of this chapter’s code, select ChangeUser.cs in the Solution Explorer. Then change its Build Action property (in the Properties panel) from None to Compile, just as you did with the UserName.cs form. Confirm that its subordinate ChangeUser.Designer.cs file also had its Build Action changed to Compile.

All of the hard work occurs in the form’s ActOK_Click event handler. If the user selects the Return to Patron Mode option, all security values are cleared, and the main form hides most features (through code added later).

LoggedInUserID = -1L; LoggedInUserName = ""; LoggedInGroupID = -1L; ReprocessSecuritySet();

This form gets connected into the application through the main form’s ActLogin_Click event. Open up the MainForm.cs file, double-click on the Login button in the upper-right corner, and add the following code to the Click event template that appears.

Insert Snippet

Insert Chapter 11, Snippet Item 12.

// ----- Prompt the user for patron or administrative mode. ShowLoginForm();

That wasn’t much code. Add the ShowLoginForm method’s code to the form as well.

Insert Snippet

Insert Chapter 11, Snippet Item 13.

private void ShowLoginForm() { // ----- Prompt the user for patron or // administrative mode. DialogResult userChoice; // ----- Prompt the user. userChoice = (new ChangeUser()).ShowDialog(); if (userChoice == DialogResult.OK) UpdateDisplayForUser(); }

Let’s also enable the F12 key to act as a login trigger. Add the following code to the switch statement in the MainForm_KeyDown event handler.

Insert Snippet

Insert Chapter 11, Snippet Item 14.

case Keys.F12: // ----- Prompt the user for patron or administrative mode. ShowLoginForm(); e.Handled = true; break;

The ShowLoginForm routine calls another method, UpdateDisplayForUser, which hides and shows various display elements on the main form based on the security profile of the current user. Add it to the MainForm class code. I won’t show the code here, but basically it looks at the LoggedInUserID variable, and if it is set to −1, it hides all the controls for advanced features.

Insert Snippet

Insert Chapter 11, Snippet Item 15.

Currently, when you run the application, all the advanced features appear, even though no administrator has supplied an ID or password. Calling UpdateDisplayForUser when the main form first appears solves that problem. Add the following code to the end of the MainForm_Load method.

Insert Snippet

Insert Chapter 11, Snippet Item 16.

// ----- Prepare for a patron user. UpdateDisplayForUser();

The last update (five updates, actually) involves limiting the major sections of the form to just authorized administrators. For instance, only administrators who are authorized to run reports should be able to access the reporting panel on the main form. Locate the TaskReports method in the main form, and find the line that displays the panel.

PanelReports.Visible = true;

Replace this line with the following code.

Insert Snippet

Insert Chapter 11, Snippet Item 17.

if (SecurityProfile[(int)LibrarySecurity.RunReports]) PanelReports.Visible = true;

We need to do the same thing in the TaskCheckOut, TaskCheckIn, TaskAdmin, and TaskProcess methods. In each case, look for the following line.

Panel???.Visible = true;

Replace each line with code that checks the security settings before showing the panel.

Insert Snippet

Insert Chapter 11, Snippet Items 18 through 21.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.