Click here to Skip to main content
13,200,593 members (54,561 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
1. I have 8 buttons, each performs a different task, i.e. edit, delete, create etc, and a context​Menu for each of the task

2. I've a table called *Moderations* in DB, which consists of bools i.e. groupTitle, canEdit, canDelete, canCreate..... groupTitle is string not bool

3. I have a bool function *canDoIt(task, userid)* to check whether the logged in user (which will have specific groupTitle), can perform or can't (function return true or false for provided task, in short)

Suppose, I want to check whether a logged in user can perform the task or not, check via canDoit(task, userid), and If he cannot, the button will be disabled otherwise won't....
OnForm_Load I throw the function (or may be another time when I need it) and check for each button, i.e.

btnEdit.Enabled = canDo("canEdit", userID)
btnDelete.Enabled = canDo("canDelete", userID)
btnCreate.Enabled = canDo("canCreat", userID)

cnxMenuEdit.Enabled = canDo("canEdit", userID)
cnxMenuDelete.Enabled = canDo("canDelete", userID)
.
.
.
.....and so on and so forth.

My method work fine and good but I have doubts and questions....

First question, is good to be so?
Second question, is it professional?
Another is, will that effect program or database performance?

Sorry if same has already asked/posted.....

What I have tried:

btnEdit.Enabled = canDo("canEdit", userID)
btnDelete.Enabled = canDo("canDelete", userID)
btnCreate.Enabled = canDo("canCreat", userID)

cnxMenuEdit.Enabled = canDo("canEdit", userID)
cnxMenuDelete.Enabled = canDo("canDelete", userID)
.
.
.
.....and so on and so forth.
Posted 12-Oct-17 4:22am
Updated 12-Oct-17 4:34am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Your solution is fine.

This is usually done with roles or users. If you're worried about hitting the database too often then you can "cache" permissions, but if there are permissions for just about any action then it may be best to only load permission sets for that user / role for that page in the form_onload, as you have done.

I'm sure there are other clever ways of doing this but I have no issue with this.
  Permalink  
Comments
Kashif Ahmad Khan 12-Oct-17 10:51am
   
First, it's WinForm, I'm talking about and I don't know how to cache permission in WinForm. Secondly each form is of different type and different permissions/tasks.... I'm afraid too many hittings will effect database and application
Andy Lanng 12-Oct-17 10:59am
   
I get that it's win forms. To cache permissions you just load them all into memory, any way you want. If it's client/ server you can do it server side, if it's client only then just load them in the client.
If you want to load them per form the you will need a way to select out of the db all the permissions for that form at once. That way you're not hitting the database as often
Kashif Ahmad Khan 12-Oct-17 11:11am
   
Okay thanks....
(It's only client)
Will that be better to load all permissions in a DataTable or DataSet at once may be onload, and whenever and wherever needed in that form it may be used....?
Andy Lanng 12-Oct-17 12:17pm
   
Not really. Either is a good solution. Store it in a DataTable or DataSet if you like. I doubt you'll have any concerns with such variable type in a client only app.
Sounds like you have a sound plan now. Good luck ^_^
Kashif Ahmad Khan 13-Oct-17 10:10am
   
Thnaks mate....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 12 Oct 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100