Skip to content

Commit

Permalink
Removed try/catch from VerifyUserAccessToResource exception (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabricio Catae committed Feb 22, 2017
1 parent 8e9ffa5 commit 229acae
Showing 1 changed file with 68 additions and 31 deletions.
99 changes: 68 additions & 31 deletions src/Arda.Permissions/Repositories/PermissionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,44 +212,81 @@ public void DeleteUser(string uniqueName)
}
}

public bool VerifyUserAccessToResource(string uniqueName, string module, string resource)
CacheViewModel GetUserPermissionsCached(string uniqueName)
{
CacheViewModel cachedView = null;
byte[] arraySerializedCached = null;

if (uniqueName == null)
throw new ArgumentNullException("GetUserPermissionsCached(uniqueName: null)");

try
{
var propertiesSerializedCached = Util.GetString(_cache.Get(uniqueName));
if (propertiesSerializedCached != null)
{
var permissions = new CacheViewModel(propertiesSerializedCached).Permissions;

var perm = (from p in permissions
where p.Resource.ToLower().Equals(resource) && p.Module.ToLower().Equals(module)
select p).First();
arraySerializedCached = _cache.Get(uniqueName);
}
catch(StackExchange.Redis.RedisConnectionException)
{
// Ignore transient network issues
}

if (perm != null)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
if( arraySerializedCached != null )
{
var propertiesSerializedCached = Util.GetString(arraySerializedCached);
cachedView = new CacheViewModel(propertiesSerializedCached);
}
catch (Exception ex)

return cachedView;
}

public bool VerifyUserAccessToResource(string uniqueName, string module, string resource)
{
// THIS IMPLEMENTATION IS WRONG!
// If the cache is clean, then it will not properly verify the user access

//try
//{
// var propertiesSerializedCached = Util.GetString(_cache.Get(uniqueName));
// if (propertiesSerializedCached != null)
// {
// var permissions = new CacheViewModel(propertiesSerializedCached).Permissions;

// var perm = (from p in permissions
// where p.Resource.ToLower().Equals(resource) && p.Module.ToLower().Equals(module)
// select p).First();

// return (perm != null);
// }
// else
// {
// return false;
// }
//}
//catch (Exception ex)
//{
// if (ex.Message == "Sequence contains no elements")
// {
// return false;
// }
// else
// {
// throw;
// }
//}

var cachedView = GetUserPermissionsCached(uniqueName);

if (cachedView != null)
{
if (ex.Message == "Sequence contains no elements")
{
return false;
}
else
{
throw;
}
var permissions = cachedView.Permissions;

var perm = (from p in permissions
where p.Resource.ToLower().Equals(resource) && p.Module.ToLower().Equals(module)
select p).First();

return (perm != null);
}

return false;
}

public bool VerifyIfUserIsInUserPermissionsDatabase(string uniqueName)
Expand Down

0 comments on commit 229acae

Please sign in to comment.