// create a new user internal static void newUser(string id, string pass, GPLCAuthority auth) { try { using (SQLiteCommand sql = new SQLiteCommand("INSERT INTO User values(@id, @pass, @auth)")) { sql.Parameters.Add("@id", DbType.String).Value = id; sql.Parameters.Add("@pass", DbType.String).Value = CryptoUtil.encryptSHA1(pass); sql.Parameters.Add("@auth", DbType.String).Value = auth.ToString(); SQLiteDBMS.execUpdate(sql); } } catch (SQLiteException ex) { switch (ex.ErrorCode) { case 1: createSchema(); newUser(id, pass, auth); break; case 19: default: break; } } }
// check if the user has the authority equal to or higher than a specific auth public void Authenticate(GPLCAuthority auth) { if (authority > auth) { throw new UnauthorizedException(auth); } }
/* check if current user has the authority to do operation * return true or false * */ public static bool AuthVerify(GPLCAuthority auth) { try { Auth(auth); return(true); } catch (UnauthorizedException) { return(false); } }
// get auth by id and password public void Authenticate(string id, string pass) { this.id = id; this.pass = pass; try { /* create user and set its authority * SecureUtil.newUser(id, pass, GPLCAuthority.Administrator); * */ // verify id and passwod _authority = SecureUtil.Authenticate(id, pass); } catch (WrongIdPassException ex) { _authority = GPLCAuthority.Anonymous; throw ex; } }
/* check if current user has the authority to do operation * throw exception when current user has no enough authority * */ public static void Auth(GPLCAuthority auth) { user.Authenticate(auth); }
public UnauthorizedException(GPLCAuthority authority) { this.authority = authority; }
// get auth of anonymous public void Authenticate() { _authority = GPLCAuthority.Anonymous; }