StrataFrame Forum

Is it possible to temporarily apply admin or last logged in user rights in order to close forms?

http://forum.strataframe.net/Topic20813.aspx

By Cory Cookson - 11/20/2008

Scenario:

1) User 1 logs in with admin rights and opens up form1

2) Lock Screen happens due to timeout or pushing F11

3) User 2 logs in with lower rights than User 1

4) Here I want to close form1 so that User 2 has a fresh start with no open screens.

Question:

Is there a way to temporarily provide admin or the last users rights so that open forms can be closed programatically and then return to using User 2's rights?

By Ivan George Borges - 11/20/2008

Hi Cory.

Guess you could add a handler to AfterSessionLock and take care of your open forms in it. But what if you have uncommited changes to be saved?

Maybe, in your MDI form Load:

AddHandler MicroFour.StrataFrame.Security.SessionLock.AfterSessionLock, AddressOf HandleAfterSessionLock

Then:

    Public Sub HandleAfterSessionLock(ByVal sender As Object, ByVal e As EventArgs)
        '-- check if there are open forms and take your actions


    End Sub

By Trent L. Taylor - 11/20/2008

And/or the CurrentUserChanged event:

MicroFour.StrataFrame.Security.SecurityBasics.CurrentUserChanged

There is also a BeforeSessionLocked event.  Between all of these you should be able to implement your logic.

By Cory Cookson - 11/20/2008

These are great suggestions.  Thank you both.

Let me try to explain again. 

If a user with the same rights logs in after me they should see all of the forms still open.  However, if a user with less rights logs in then all forms should close for that user. 

This is an issue because by the time the CurrentUserChanged or AfterSessionLock events fire the new user does not have rights to close the open forms.

Is there a way to temporarily create and login an admin user behind the scenes to close the open forms and then restore the user logging in or a way to temporarily elevate the users rights to admin, close the forms and then return the users rights to what they were before i modified them?

or.....am I missing something sillyHehe

By Cory Cookson - 11/20/2008

I have gotten this far in the AfterSessionLock event:

SFSUsersBO sfs = new SFSUsersBO();

sfs.FillByUserName(_LastUserLoggedIn);

SecurityBasics.CurrentUser = MicroFour.StrataFrame.Security.LoggedInUser.CreateNew(sfs);

This gives me back the user who had previously logged in but the form I want to close is still disabled and will not close w00t

As always all feed back is appreciated.

By Greg McGuffey - 11/20/2008

I'd look into a custom login form. Then you could set a flag that the session timed out (before session timed out event), then test for that when the user logs back in...they'll use your login form to do it, so you could test there if the user is changing after a time out and handle the forms before logging the new user in.



This also suggests an enhancement request: CurrentUserChanging event, which might be cancelable.
By Cory Cookson - 11/21/2008

This doesn't make sense to me.  I'm able to switch users on the fly and the particular user I am swapping to does show as admin.

Is there a way at this point to tell security to reload so that the forms are not disabled?

It just feels like I'm missing something simple here (or not explaining myself well enough) Hehe

By Ivan George Borges - 11/21/2008

Hi.

I see you worked your code in the AfterSessionLock. Have you tried managing your users in the BeforeSessionLock too?

By Cory Cookson - 11/21/2008

yes, in the beforesessionlock I make sure that any form that has unsaved changes gets taken care of so that in the afterSessionLock event all that should have to happen is I close the open forms if it is not the same user.
By Ivan George Borges - 11/21/2008

OK. Now, in the AfterSessionLock, before dealing with your open forms, have you tried something like:

Login.SetLoggedInUser("YourAdminUser", "YourAdminPassword", "")

 

By Cory Cookson - 11/21/2008

yes it doesn't seem to affect the fact that the form has already been disabled.
By Cory Cookson - 11/21/2008

yes I've tried it but it doesn't seem to reset the security on the forms so that they are no longer disabled.
By Cory Cookson - 11/23/2008

bump
By Ivan George Borges - 11/23/2008

Hi Cory.

Have you also tried Greg's advice, and worked on your Login form? Since we are fighting the normal behavior of the SessionLock, working on your Login form could give you some flexibility to take care of actions.

By Dustin Taylor - 11/24/2008

Once you change the user you will have to call LockSession again to refresh the form states. You'll have to account for this in your AfterSessionLock logic, however, to prevent it from getting into an infinite loop.

That said, this isn't what I would recommend to handle the issue of unclosable locked forms. The session locking works the way it does for a reason, namely that users lower privileges should not be able to tamper with the work done by users of higher privileges (including discarding changes made by the administrator). That is why they have the different levels, to prevent one from having access to the other.  As such, if you want to avoid the situation where an administrator logs out, and then a standard users logs in and can't close some open forms, I would recommend enforcing the administrator to save and close their work before logging out, rather than upping the permissions of the standard user to allow them access to explicitly denied forms or functions.

By Cory Cookson - 11/24/2008

Thank you for the replies!  I really appreciate the support of the forum.

I am making sure that the first user has saved or reloaded all data that they are working on before they are logged out.

I've tried recalling LockSession in the afterSessionLock event and it just brings up the login form again.

I want to close the open forms without the second user even knowing they were open. 

There are however times that another person will login with equivelant rights and they should be able to see all of the screens that were left open by the first user.

As always any help is appreciated Smile

By Cory Cookson - 11/25/2008

I believe that I've found a solution to the issue.  Code snippets as follows:

public static void HandleCurrentUserChanged(object sender, EventArgs e)

{

if (_UserID.Length > 0)

{

_UserChanged = (_UserID != SecurityBasics.CurrentUser.UserName.ToString());

}

_UserID = SecurityBasics.CurrentUser.UserName.ToString();

if(_UserChanged)

{

for (int i = 0; i <= Application.OpenForms.Count - 1; i++)

{

MicroFour.StrataFrame.UI.Windows.Forms.BaseForm bf = (MicroFour.StrataFrame.UI.Windows.Forms.BaseForm)Application.OpenForms[i];

if (bf.Name != "Form1")

bf.Close();

}

}

}

private static void HandleBeforeSessionLock(object sender, EventArgs e)

{

for (int i = 0; i <= Application.OpenForms.Count - 1; i++)

{

MicroFour.StrataFrame.UI.Windows.Forms.BaseForm bf = (MicroFour.StrataFrame.UI.Windows.Forms.BaseForm)Application.OpenForms[i];

if (bf.Name != "Form1")

{

if (bf.EditingState == MicroFour.StrataFrame.Business.BusinessEditingState.Editing)

{

if (MicroFour.StrataFrame.Messaging.MessageForm.ShowMessage("unsaved data detected",

"Do you want to save your changes to " + bf.Name, "", MicroFour.StrataFrame.Messaging.MessageFunction.YesNo, MicroFour.StrataFrame.Messaging.MessagingIcon.Question,

MicroFour.StrataFrame.Messaging.MessagingSounds.DefaultSound) == MicroFour.StrataFrame.Messaging.MessageFunctionType.Yes)

bf.Save();

else

bf.Undo();

}

}

}

}

By Ivan George Borges - 11/25/2008

Great, Cory! Cool