C# management a grants -
i have problem management grants user enabled two/three different areas, example, user profile don't have problems:
user = foo area = east level = 2
instead, user profile:
user = pluto area = east area = west level = 2
the statement users() performs access control on table of authorized users according area of membership , expected level (0, 1 , 2) takes account west area user pluto, rather enable both area east , west.
my code below.
any appreciated, in advance.
protected void users() { using (odbcconnection conn = new odbcconnection(configurationmanager.connectionstrings["cn"].connectionstring)) { sql = " select * "; sql = sql + " tblusers "; sql = sql + " (email = ? "; sql = sql + " , degree not null); "; using (odbccommand command = new odbccommand(sql, conn)) { try { command.parameters.addwithvalue("param1", server.urldecode(request.cookies["email"].value)); command.connection.open(); using (odbcdatareader reader = command.executereader()) { while (reader.read()) { degree = reader["degree"].tostring(); area = reader["area"].tostring(); } } } catch (exception ex) { throw ex; } { command.connection.close(); } } } }
edit 1
string level; string area; public class grantuser { public string area { get; set; } public string level { get; set; } public grantuser() { } public grantuser(string area, string level) { this.area = area; this.level = level; } } protected void users() { using (odbcconnection conn = new odbcconnection(configurationmanager.connectionstrings["cn"].connectionstring)) { sql = " select * tblusers (email = ? , level not null); "; using (odbccommand command = new odbccommand(sql, conn)) { try { command.parameters.addwithvalue("param1", server.urldecode(request.cookies["email"].value)); command.connection.open(); list<grantuser> lsgrantuser = new list<grantuser>(); using (odbcdatareader reader = command.executereader()) { while (reader.read()) { level = reader["level"].tostring(); area = reader["area"].tostring(); lsgrantuser.add(new grantuser(reader["area"].tostring(), reader["level"].tostring())); } } } catch (exception ex) { throw ex; } { command.connection.close(); } } } } protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { label area = (label)e.row.findcontrol("area"); if (!string.isnullorempty(level.tostring())) { if (level.tostring() == "0") { //here condition 0 } if (level.tostring() == "1") { if (area.text == area.tostring()) { //here condition 1 } } if (level.tostring() == "2") { if (area.text == area.tostring()) { //here condition 2 } } } } } public datatable gridviewbind() { sql = " select ....... ; "; try { dadapter = new odbcdataadapter(sql, conn); dset = new dataset(); dset.clear(); dadapter.fill(dset); datatable dt = dset.tables[0]; gridview1.datasource = dt; conn.open(); gridview1.databind(); if (dt.rows.count == 0) { page.clientscript.registerstartupscript(this.gettype(), "alert", "alert('no data.');", true); } return dt; } catch (exception ex) { throw ex; } { dadapter.dispose(); dadapter = null; conn.close(); } }
edit 2
users(); gridview1.databind();
if i'm understanding question, sounds have area
property take on number of different values "east", "west", , assume "north" , "south" (or similar).
in case, start enumeration so:
enum areas { east, west, north, south }
then change type of area
string
areas
:
public class grantuser { public areas area { get; set; } public string level { get; set; } public grantuser() { } public grantuser(areas area, string level) { this.area = area; this.level = level; } }
now can set grantuser.area
1 of values on list (or else won't able compile):
grantuser user = getuserfromsomewhere(); user.area = areas.east; //valid user.area = areas.elsewhere; // invalid, won't compile
finally, if want user able have several "areas" assigned them, we'll give enum [flags]
attribute, create default none
value, , assign each item value increases power of 2 (sounds confusing, once you've done few times feel normal). check "flagsattribute class" more information.
[flags] public enum areas { none = 0, east = 1, west = 2, north = 4, south = 8 }
finally, give user access both east
, west
need or areas together:
grantuser user = getuserfromsomewhere(); user.area = areas.east | areas.west; //gives value of 3, 1 + 2
notice when or powers of 2 same adding them together, because of way bits line powers of 2 (and topic). note not true in general case. i.e.: 3 | 7 == 7.
now check area
user has access to, use , operator:
if(user.area & areas.east == areas.east) {//has access east} if(user.area & areas.west == areas.west) {//has access west}
for further reading , detailed code examples, check out enumeration types c# programming guide.
Comments
Post a Comment