Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How would you go about implementing user modes into a desktop application?


  • Please log in to reply
6 replies to this topic

#1 whoiga

whoiga

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 25 August 2008 - 11:58 AM

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.
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 25 August 2008 - 12:31 PM

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 whoiga

whoiga

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 25 August 2008 - 12:39 PM

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:
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);
}
}
}

  • 0

#4 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2765 posts

Posted 26 August 2008 - 10:24 AM

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
  • 0
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"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#5 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 26 August 2008 - 10:30 AM

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.
  • 0

#6 whoiga

whoiga

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 27 August 2008 - 07:46 AM

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.
  • 0

#7 gaylo565

gaylo565

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 258 posts

Posted 27 August 2008 - 12:36 PM

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:)
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download