Web Application Opener - WebSnap (5)

zhaozj2021-02-08  209

Web Application Opener - WebSnap!

Chapter 5, Handling Management and Sessions

Under the conventional WebBroker InternetExpress architecture, we handle users control or session management. WebSnap has strengthened the ability of this part, using WebSnap to handle users or session is a very simple thing, next We add these features for our programs.

5-1 Make User Management and Sessions

To use Session and the user to control your function in the WebSnap program, you must add three components in Application Module: TendusersessionAdapter, TWebuserList, TsessionServices. TENDUSERSESSIONADAPTER relates to TendUseraDapter and added session to handle, simply she just puts the material's material into the session. TWEBUserList is responsible for keeping user profiles and verification, TsessionServices is the core component of the entire WebSNAP Application Session controller. When the web page is started, TWEBAPComponents will call TsessionServices to generate a session to visitors, then pass this session to WebContext Save . When sending Response, seal the sessionid into a cookie and send it to the utility, and the next Request will send this sessionID back to our program with cookies. With this sessionID, we can get the information in the last session, which will be affected by the save time of the cookie and the save time limit of the session. When the cookie expires or the session time limit is or you restart the program The session will disappear, the following is the process of WebSNAP processing session:

( Image 6)

When you put these components, you have to set the use of the users in TWEBUserList, please double according to her useritems characteristic value to add the utility materials and set their ID and password information:

In this example, TENDUSERSESSIONADAPTER will save the user's material in the session, and we also take a casket to use the session item. Please add an Adapterfield name in TendusersSessionAdapter to be adaptage:

After writing her an onGetValue event, add the following code:

If not varisempty (session.values ​​['agn']) THEN

Value: = session.values ​​['agn'] else value: = '';

The previous section code is used to determine if there is a value of an AGE in the session, and there is a VALUE variable. In this way, we can add this value in the Script of Home Module's script:

welcomes <% = enduser.displayName%> You are <% = enduser.adaptage.value%> years

Defining language system (in order to be used as its language network page)

After that, we have to add a Page Module to make the users to log into the system, select AdapterPageProducer and set the PageName to login, and put it in the inside a TLoginformAPter component. By our hopes, I hope that the Login is allowed to fill in the additional information, and therefore, in addition to the three Adapterfield components prepared by TLOGINFORMADAPTER, I have to add an Adapterfield component:

After completing, I have to write an onupdatevalue event to the Adaptage, and save the input value into the session:

Procedure Tlogin.AdaptageUpdateValue (Sender: Tobject; Value: Variant);

Begin

Session.Values ​​['Age']: = adaptage.actionValue.values ​​[0];

END;

After that, we can set up the Visual Page Designer to design our login network page, the entire picture is as follows:

The last time back to Application Module sets the LoginPage characteristic value of TendusersessionAdapter to login (PageName on the web page).

Then do we want to set a web page to the user to enter in advance to enter? You have two options, one is to tick the login required option when establishing the page module, and the other is manually changing the feature value of the Page Module In this example, I want to set Edit this webpage into a web page that needs to be accessed first, so I will make the following modifications in the Edit:

WebRequestHandler.AddWebmoduleFactory (TEBPAGEMODULEFACTORY.CREATE (TEBPAGEMODULEFAA.CREATE ([WPPUBLISHED, WPLOGINREQUIRED], '. Html'), crdemand, cacche);

After completing, you can perform a program to see the results. Normal words must be login to access the Edit web page, this is normal, but you have found a question: In the picture, we see that the user can change NextPage to select the login-oriented web page, this is not applicable in many cases, because we may not want the user to change this setting, we may want to judge in the program or set this value. From the current TLoginformAdapter, this nextPage property is a protection level. It also represents that we can't change her. According to Delphi R & D, it will open this property after Patch, and we can use the following techniques to complete this action. :

TProtectedLoginformAdapter = Class (TloginformAdapter);

... ... ....

TProtectedLoginformAdapter (loginformadapter1) .NextPage: = 'Home';

If you have seen my other article, then this skill should be quite familiar with you. You can place this code to the Module's Activate event, when you successfully log into the system and select Next Page for the first page of Code6421, you should see the picture:

This should be clearly used in the session, then we can save the type of information in the session? The reason is only the type supported by Variant to save the SESSION.

5-2 Controlling of the preservation period

This model is a big summation to meet the needs of the majority, but you can see it after you see the next picture surface:

In the picture, I opened a web page to log into the other, I will open another IE network page. If you can send it to you, you must go back in the second network page, which is not available in many cases. This is the result of setting the Cookie's saving period setting, which can solve this problem by the preservation period of setting cookie:

Uses dateutils;

... ..

Procedure Thome.WebappCompComponentsBeforedispatch (Sender: Tobject)

Request: TwebRequest; Response: TwebResponse; Var Handled: Boolean;

VAR

I: integer;

Begin

For i: = 0 to WebContext.Response.cookies.count-1 do

Begin

If Sametext (WebContext.Response.cookies.Items [i] .name, 'WebBrokerSessionId') THEN

Begin

IF WebContext.Response.cookies.Items [i] .Expires = -1 thenwebcontext.response.cookies.Items [i] .Expires: = incday (date);

END;

END;

END;

The top of the top, we set the Cookie's saving period to one day, then you will be normal after you start the schedule:

In addition to the problem of multiple IE window, what should our utilitarian materials are stored in the material library? Look at the following frames you will clearly:

Procedure Thome.WebuserlistBeforevalidateUser (strings: tstrings;

Var Userid: Variant; Var Handled: Boolean;

Begin

IF strings.count> 0 THEN

Userid: = strings.values ​​['username "];

Handled: = True;

END;

In the figure, I am just a single pure record that is included in the material, that is, if you can't manage anyone, you can pass the verification. You can add information from the property in this area and the amount of information that is more than the ingredients than the ingredients. When you are named UserId, you will have two values ​​in the table, and there are two values ​​in the incoming Strings parameter, one is username, the other is Password.

5-3 control control

In addition to the basic enforcement control, WebSnap also provides control control, all of which are all from Page to ACTION, you can try to set up a control control of a Page's xxxccess characteristic value.

WebRequestHandler.AddWebmoduleFactory (TWEBPAGEModuleFactory.create (TMasterDetailedit,

TWEBPAGEINFO.CREATE ([WPPUBLISHED {, WPLoginRequired}], '.html'), crdemand, cacche);

Constructor TWEBPageInfo.create (aaccess: twebpageAccess; const apagefile;

Const Acaption, ADESCRIPTION, AVIEWACCESS: STRING

There is an event in TWebuserList. After matching the above program, you can construct a complete user control function! In addition to Adapter, WebPageInfo, Adaptection, we can also set an Adapter Control Items Different of permissions make a correct response. For example, you can set the viewmode of tadapterdisplayfield for VMToggleonAccess, this Control will change in the way without the modify power limit, which is transformed into the EDIT when the user has a Modify permission. It seems that it seems very good, in fact, she has a problem, just like our MasterDetailedIT webpage is a renovation mode after entering the page, and there is no error in the beginning. The display of pure text, but you don't forget, we set Adaptermode to Edit, which also represents our mobile records to trigger Update's actions. In this UPDATE card, you call CheckModify, because the user has not been renovated, of course, the exception is jumped out, then we can't use this feature? Oh! Mountain does not turn the road, we can MasterDetailedit.onbeforeDispatchPage event plus processing permissions: Procedure TMasterDetailedit.WebPageModuleBeforeDISPATCHPAGE (

Sender: TOBJECT; Const PageName: String; Var Handled: Boolean;

VAR

UserItem: TwebuserItem

EditMode: String;

Begin

UserItem: = Home.WebuserList1.UserItems.FindUserName (Home.endusersessionAdapter1.Userid);

EditMode: = '';

IF assigned (useritem) THEN

Begin

IF userItem.accessrights = wdmdata.dsadaptorder.modifyAccess Then

EditMode: = 'edit';

END;

AdapterfieldGroup1.adaptermode: = Editmode;

Adaptergrid1.adaptermode: = editmode;

END;

Once, you can change the viewmode of the AdapterEditcolumn and AdapterDisplayField to VMToggleonAccess can be implemented. It is not a bug, but it is only the equation that is not complete, but it is still not enough to complete the entire MasterDetailed 权 权 权 权 权 控 控 控 权 控 控 控 控 控 控 控 控 控 控 控 控 控 控 控 控 控 控 控 控 控 控 控 控Although it will generate exceptions, it will not be more information, but it is always not pair. In fact, we can use the AdapterAction.ExecuteAccess feature value in datasetadapter.Actions and the ActionButton.hiddenOptions feature value [bhohiddonnoexecuteaccess], so You can determine the right to hide or displays the message, the MASTEREDITDETAIL page in the example has been configured. You can use the Code6421 password to be 1234 login, you can modify it. If you don't log in, you can only see pure. Text display. 5-4 Put the session in the archive

Controlling Cookie's preservation period is a simpler method, but this is a defective point of life. When our web program is turned off (refers to removal from the memory, for example, you will restart the web server or computer), because the session is stored in the memory, so it has also disappeared with the process of stopping . At this time, the cookie reserved in the user has no effect. To solve this problem, we have two options, the simpler method is before the program is stopped, and the entire sessions are deposited into the database. After reading the database, return to the sessions item. This approach has a shortcomings because of the storage of the entire sessions, which can save more information, such as temporary variables, or some roots that are not worth saving, this is a lot of memory and space. The second way is that only the sessions currently used, this method is better than the former, but there is still the same problem, but the problem is simple, because we only store one session, so the waste of the waste is limited to this session In the object, it is much better than the first method. The example of this section is Demo2, please open the scenario.

The concept of the whole process is coming from Nick Hodges, I take the program of the program, integrate her into my temple. First, I will introduce two tools in HOME Page Module: // Write a session to a stream

Procedure Thome.sAvestness (AID: TSessionID; ASTREAM: TSTREAM);

VAR

TempSesions: tessions;

Tempitem: tsessionitem;

Begin

Tempations: = tiesions.create;

Try

Tempitem: = TessionItem.create (TempSess);

IF sessions.getations (AID, TEMPITEM) THEN

TempSessionS.SaveTroupTream (Astream)

Else

Assert (False, 'Session Not Found');

Finally

TempSessionS.Free;

END;

END;

// update or add all name / value pairs from the saved session to a new or existing session

Procedure Thome.RESTORESession (AID: TSessionID; ASTREAM: TSTREAM);

VAR

TempSesions: tessions;

Item: tsessionitem;

I: integer;

Begin

If astream.size <> 0 THEN // if there is no data there, do't do anything

Begin

Tempations: = tiesions.create;

Try

TempSessionS.LoadFromstream (astream);

IF AID = '' THEN

// Create a new session

AID: = SESSCOLN.SESSIONS.STARTSESSION

Else

Assert (SESSIONS.SESSIONEXISTS (AID), 'Could Not Find Session' AID);

Item: = TempSessionS.Items [0] as tsessionItem

For i: = 0 to item.Items.count - 1 DO

Sessions.SetItemValue (AID, Item.Items.Names [i], item.Items.variants [i]);

Finally

TempSessionS.Free;

END;

END;

END;

The function of the Savesession letter is to remove the session removal of the SessionID in Sessions, because the session object itself does not provide SAVETOSTREAM function, so Nick has established a temporary sessions object and will The acquired session is copied to the temporary session object, and finally call SaveTroupTream to store the session object to Stream.

RESTORESSESSSION is completely opposite to Savesession, and her function is to take the session item from Stream, and then return her to the SESSION of the current line. With these two tools, we have written more than enough. Let me first plan to use these two letters to build the SESSION to the subscription. The following is a flow chart of our program: When the user first logs in, she must fill in the information in the page of the personal information. In this page, we have made a certain letter, and we will use the database. And the SAVESession group to store the session into the database. When this is the second time you log in, the system can take the last session material from the property library, and then use the restoresession letter to return her back to the session item. We construct a new table, build three fields, user_id, session_id, session_info, user_id used to store the user login ID, session_id is the sessionID generated by the storage user after logging in the system, session_info is in stock SESSION objects.

At our time, we have to write a form code for the SESSION to the material library, and we choose to write the owner to fill in personal information and press it.

Procedure tprofile.adaptsaveprofileexecute (sender: Tobject)

Params: tstrings;

PROCEDURE SAVESession;

VAR

S: TSTREAM;

Begin

if not dmdata.tbsessionfile.active the

DMDATA.TBSESSIONFILE.OPEN;

If DMDATA.TBSESSIONFILE.LOCATE ('user_id', webcontext.enduser.displayName, []) THEN

Dmdata.tbsessionfile.edit

Else

DMDATA.TBSESSIONFILE.APPEND;

Dmdata.tbsessionfile.fieldbyName ('user_id'). value: = WebContext.enduser.displayName;

Dmdata.tbsessionfile.fieldbyName ('session_id'). value: = session.SessionID;

S: = dmdata.tbsessionfile.createblobstream (DMData.tbsessionFile.fieldByName ('session_info'), bmwrite;

Try

Home.SAVESession (Session.SessionID, S);

DMDATA.TBSESSIONFILE.POST;

Finally

S.free;

END;

END;

Begin

Adapter1.UpdateRecords (WebContext.AdapterRequest as IActionRequest); Savesession;

END;

After logging in the use, if it is determined that the user is the second logo, we have to take the session and still write the session, I chose to write her in the WebuserList.OnbeforevalidateUser event:

Procedure Thome.WebuserlistBeforevalidateUser (strings: tstrings;

Var Userid: Variant; Var Handled: Boolean;

Procedure loadSession (AUSERID: STRING);

VAR

S: TSTREAM;

Begin

IF WebUserList.UserItems.count = 0 THEN

EXIT;

if not dmdata.tbsessionfile.active the

DMDATA.TBSESSIONFILE.OPEN;

If DMDATA.TBSESSIONFILE.LOCATE ('user_id', auserid, []) THEN

Begin

S: = dmdata.tbsessionfile.createblobstream (DMData.tbsessionFile.fieldbyName ('session_info'),

bmread);

Try

RESTORESession (session.SessionID, S);

Finally

S.free;

END;

END;

END;

VAR

UItems: twebuseritem;

Begin

// Process User Login

If not dmdata.tbemployee.active dam

Dmdata.tbemploye.open;

If DMDATA.TBEMPLOYEE.LOCATE ('firstname; Empno', varings.values ​​['username'],

Strings.values ​​['Password']]), []) THEN

Begin

UITems: = WebuserList.UserItems.Add as TwebuserItem

UItems.username: = strings.values ​​['username "];

UItems.password: = strings.values ​​['password'];

UITEMS.ACCESSRIGHTS: = '';

LoadSession (UITEMS.USERNAME);

END;

END;

In this way, we have completed the program of saving sessions. This program has a small shortcoming, that is, the scope of storage is still too wide, and there will be some temporary materials in the session, we don't want to keep this information, but this The program will still be stored. In the article, I will write this approach to the article, let her only save the information you need.

This chapter

In this section, I discussed the intensive management and session and other technologies. In this way, the upper half of this article is also embarrassed, and we will enter another stage, detailedly exploring the fine line of WebSnap and some more advanced applications.

转载请注明原文地址:https://www.9cbs.com/read-3106.html

New Post(0)