I'm planning a project for a desktop application that will communicate with a database. I want to incorporate administrative features into the application. Essentially, I'll have two user modes: User, and Admin. A user will have access to necessary form controls & functionality. An admin will have the same access that a user does, but will have access to additional form controls and functionality.
My problem thus far in the designing phase is that I can't seem to wrap my head around a method of implementation that would keep the code clean and maintainable, while remaining somewhat easy.
I thought about setting up an enum, and having my user modes within. When a user connects to the database and logs into their account, depending on what flags their account has for their access in the database (perhaps indeed an enum field), the application would set the enum. Then, I would have a function that updates the states of the form controls (and other features) based on the enum, so like... certain context menu items might be disabled, certain textboxes and other controls might become readonly, etc.
However, I'm not entirely sure that such a methodology will keep things simple and maintainable.
So here are my two questions:
How might you go about it?
If you're not sure, might you know of a good resource (online or text) that I should take a look at? Thus far I've not found very many resources for doing something similar to what I have in mind.
-Martin
What you've come up with will probably work. Realize that you have limited yourself to 1) a fixed number of user levels and 2) user level restrictions are hard-coded. User-level access tends to be somewhat non-simple by its nature.
Thanks for the feedback. I suppose you're right. I know that I'm sort of limiting myself, but the simple nature of the application I want to create should void the need for additional user modes, though if I did need some, I'd only have to adjust a single method, and if I needed more/less controls, it's just a matter of adding/modifying/removing what happens to them in that method.
After I posted this thread, I made a quick little application in VS2008 to test my logic and get a feel for what it would be like. It's not the most scalable solution, but as I said the application I want to develop is simple in its nature and it would actually benefit from retaining some simplicity.
Here's the MainForm.cs code of the little demo I just created:
[highlight=csharp]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EnumUserModeTest
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
SetControls(Mode.User);
}
public enum Mode
{
User,
Admin
}
private void SetControls(Mode appmode)
{
switch (appmode)
{
case Mode.Admin:
this.Text = "Mode: Admin";
tbUserMessage.ReadOnly = false;
buttonUserMessage.Enabled = true;
tbAdminMessage.ReadOnly = false;
buttonAdminMessage.Enabled = true;
break;
case Mode.User:
this.Text = "Mode: User";
tbUserMessage.ReadOnly = false;
buttonUserMessage.Enabled = true;
tbAdminMessage.ReadOnly = true;
buttonAdminMessage.Enabled = false;
break;
}
}
private void buttonUserMessage_Click(object sender, EventArgs e)
{
MessageBox.Show(tbUserMessage.Text, "User Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void buttonAdminMessage_Click(object sender, EventArgs e)
{
MessageBox.Show(tbAdminMessage.Text, "Admin Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void userToolStripMenuItem_Click(object sender, EventArgs e)
{
SetControls(Mode.User);
}
private void adminToolStripMenuItem_Click(object sender, EventArgs e)
{
SetControls(Mode.Admin);
}
}
}
[/highlight]
-Martin
if i was facing your situation, i would choose a very simple approach, by making one common interface (or forms) and i would make a small form with all the admin controls that opens when the user is admin only...
but i think yours is better
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
I would just put all the controls for every level of user out there. And then just have everything check a user_mode variable before they do anything to check if it has the correct clearance.
I suppose I could do that, though I think it wouldn't be as clean, and it might seem disheartening or annoying for a user to constantly find out what he/she can't do, which might make the application more difficult to learn or use.
I have considered having a separate form with administrative features residing within it, though I'd still be using an enum & a control adjustment method to render it accessible/inaccessible based on the user's level.
Oh well. I'll find out what works (and what doesn't) soon enough.
-Martin
I would create a base class then a method for a general user sign in as well as one for the admin. The methods would add the appropriate controls to your form as well as the text for a welcome message or anything else unique to the user. Generally when you create a user system you create a sub-folder for each new user so that permissions for files and other information stored by the user can be set with ease. This would only add a step or two more to your code but I believe it would make maintaining your app much easier in the future![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks