Wednesday, April 15, 2009

Access Control For Forms

So you want access for each of your form in your application

Here is the answer

  1. This is your new form inherited from form
   1: using System;
   2: using System.Collections.Generic;
   3: using System.ComponentModel;
   4: using System.Drawing;
   5: using System.Data;
   6: using System.Text;
   7: using System.Windows.Forms;
   8: using System.Diagnostics;
  10:  
  11: namespace Master
  12: {
  13:     public partial class AdvForm : Form
  14:     {
  15:         public AdvForm()
  16:         {
  17:          
  18:             InitializeComponent();
  19:         
  20:         }
  21:  
  22:         
  23:         public void AccessShow()
  24:         {
  25:             string p = this.Name;
  26:             Access acc = new Access();
  27:             if (acc.HasAccess(p, acc.getCurrentUserGrupID()))
  28:             {
  29:                 Show();
  30:             }
  31:             else
  32:             {
  33:                 MessageBox.Show("Access Denied");
  34:                 this.Close();
  35:             }
  36:  
  37:         }
  38:  
  39:  
  40:     }
  41: }

2. This is the access class

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4: using System.Windows.Forms;
   5: using System.Collections;
   6: using System.Reflection;
   7: using System.Security.Permissions;
   9:  
  10: namespace Master
  11: {
  12:     public class Access
  13:     {
  14:         public BitArray FindUsersBitArray(int no)
  15:         {
  16:             BitArray ba = new BitArray(new int[] { no });
  17:             return ba;
  18:         }
  19:         public int FindUsersInt(BitArray ba)
  20:         {
  21:             int[] array = new int[1];
  22:             ba.CopyTo(array, 0);
  23:             return array[0];
  24:         }
  25:         [EnvironmentPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
  26:         public int getCurrentUserGrupID()
  27:         {
  28:             IYetkiGrup oYetki = Util.Create<IYetkiGrup>();
  29:             return oYetki.GetYetkiGrupIDByKisiID(Util.Kullanici.KullaniciID);
  30:         }
  31:         [EnvironmentPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
  32:         public  bool HasAccess(string frmName, int currentGrupId)
  33:         {
  34:             IYetkiGrup oYetki = Util.Create<IYetkiGrup>();
  35:             int yetki = oYetki.GetFormYetkiIntByFormName(frmName);
  36:             return FindUsersBitArray(yetki)[currentGrupId];
  37:         }
  38:     }
  39: }

Here is how it works:

As you can see from advForm users will not use the form.show but accessShow it will check if the user has the acces

   1: public int GetFormYetkiIntByFormName(string formName)
   2: {
   3:     DSFormYetki dsFormYetki = GetFormYetkiByFormName(formName);
   4:     if (dsFormYetki.FormYetki.Rows.Count == 0)
   5:     {
   6:         SaveFormYetki(formName);
   7:         return 0;
   8:     }
   9:     else
  10:     {
  11:         return (int)dsFormYetki.FormYetki[0].Yetki;
  12:     }
  13: }

We save the formName and an int value whether indicating the user has access or not when the program runs and user tries to open a form inherited from adv form program will check if the form is in the database if not it will save the form name so the user access menu(which is not currently here) will add it to list of forms.If the database has the form name its integer value will tell us if the user has access or not.

If we use a 32 bit integer we will be able to use access control for up to 32 users FindUsersBitArray converts an int to a bit array and every bit will be a user if the bit is 0 user wont have access and if 1 user will have access.

Ex: Form1 9 will mean ;the user 3  and the user 0  will have access to Form1.

No comments: