/// <summary> /// Changing an access record for our DB /// </summary> /// <param name="_user"></param> /// <param name="_new_level"></param> public void ChangeAccess( User.User _user, Access.AccessLevel _new_level ) { foreach ( var ac in accesses ) { if ( ac.User.Credentials.Login == _user.Credentials.Login ) { ac.SetLevel(_new_level); ac.Save(); } else this.AddAccess( _user, _new_level ); } }
/// <summary> /// Reading an access file to an array of "Access" objects /// </summary> /// <param name="dwarf_obj">An object, for which we need to get a set of accesses</param> /// <param name="_user"></param> /// <returns></returns> public static Access[] ReadAccessFile( object dwarf_obj, User.User _user ) { var ret = new Access[0]; var acc_filepath = GetAccessFilePath( dwarf_obj ); if ( !File.Exists( acc_filepath ) ) { CreateAccessFile(acc_filepath); } string contents = String.Empty; using ( var fs = new FileStream( acc_filepath, FileMode.Open, FileAccess.Read ) ) { var sr = new StreamReader( fs ); contents = sr.ReadToEnd(); sr.Close(); } var contents_splitted = contents.Replace("\r",String.Empty).Split('\n'); foreach ( var strg in contents_splitted ) { var tmp = strg.Split(':'); if ( tmp.Length == 2 ) { Access.AccessLevel al = Access.AccessLevel.DENIED; switch ( tmp[1] ) { case "ADMIN": al = Access.AccessLevel.ADMIN; break; case "READ_ONLY": al = Access.AccessLevel.READ_ONLY; break; case "READ_WRITE": al = Access.AccessLevel.READ_WRITE_DROP; break; case "READ_WRITE_DROP": al = Access.AccessLevel.READ_WRITE_DROP; break; } var ret_acc = Access.Instance( _user, al, dwarf_obj ); Array.Resize(ref ret, ret.Length+1); ret[ret.Length-1] = ret_acc; } } return ret; }
private static bool CheckLevel( object dwarf_obj, Access.AccessLevel level, User.User user ) { if ( dwarf_obj != null ) { var chk_lvl = Access.AccessLevel.DENIED; if ( dwarf_obj is DataContainer ) { var obj = ( dwarf_obj as DataContainer ); chk_lvl = obj.GetLevel( user ); } else if ( dwarf_obj is DataBase ) { var obj = ( dwarf_obj as DataBase ); chk_lvl = obj.GetLevel( user ); } else return false; return chk_lvl == level; } else throw new AccessException( " dwarf_obj is NULL in CheckAccess.CheckLevel!!! " ); }
/// <summary> /// Changing an access record for our DB /// </summary> /// <param name="_user">User</param> /// <param name="_new_level">Required access level</param> public void ChangeAccess(User.User _user, Access.AccessLevel _new_level) { local_am.ChangeAccess(_user, _new_level); }
/// <summary> /// Adding a new access record for our DB /// </summary> /// <param name="_user"></param> /// <param name="_level"></param> public void AddAccess(User.User _user, Access.AccessLevel _level) { local_am.AddAccess(_user, _level); // If we're setting admin rights => we need to set it on all of // DCs under this DB UpdateDataContainers(); if (_level == Access.AccessLevel.ADMIN) { foreach (DataContainer dc in inner_dc_dict.Values) { dc.AddAccess(_user, _level); } } }
public void Save( Access _acc ) { CreateAccessFile(); using ( var fs = new FileStream( filepath, FileMode.Append, FileAccess.Write ) ) { var sw = new StreamWriter( fs ); sw.WriteLine( _acc.User.Credentials.Login + ":" +_acc.Level ); sw.Close(); } }
/// <summary> /// Adding a new access record for our DataStructure /// </summary> /// <param name="_user"></param> /// <param name="_level"></param> public void AddAccess( User.User _user, Access.AccessLevel _level ) { var t = Access.Instance( _user, _level, acc_object ); accesses.Add(t); }
/// <summary> /// Getting an instance for given user, level and object /// </summary> /// <param name="_user"></param> /// <param name="_level"></param> /// <param name="_object"></param> /// <returns></returns> public static Access Instance( User.User _user, AccessLevel _level, Object _object ) { // Let's look in our array. Do we already have a such object? foreach ( var al in instances ) { if ( al.User.Credentials.Login == _user.Credentials.Login && al.Level == _level && al.AccessObject == _object ) { return al; } } // If we don't have => let's create a new Access instance and add it to our array var new_acc = new Access( _user, _level, _object ); instances.Add( new_acc ); new_acc.Save(); return new_acc; }