2011-06-29 20:27:07 +00:00
|
|
|
|
using System.Collections.Generic;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
using Tanshu.Accounts.Entities.Auth;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
using System.Linq;
|
2011-02-09 12:03:22 +00:00
|
|
|
|
using Tanshu.Accounts.Repository;
|
2011-01-06 07:17:00 +00:00
|
|
|
|
|
2011-02-09 12:03:22 +00:00
|
|
|
|
namespace Tanshu.Accounts.Contracts
|
2011-01-06 07:17:00 +00:00
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
public static class Session
|
2011-01-06 07:17:00 +00:00
|
|
|
|
{
|
2011-04-11 12:55:45 +00:00
|
|
|
|
private static Dictionary<string, bool> _roles;
|
|
|
|
|
private static User _currentUser;
|
2011-01-06 07:17:00 +00:00
|
|
|
|
public static bool IsAuthenticated { get; private set; }
|
2011-01-30 07:14:05 +00:00
|
|
|
|
public static User User
|
2011-01-06 07:17:00 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2011-04-11 12:55:45 +00:00
|
|
|
|
return _currentUser;
|
2011-01-06 07:17:00 +00:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
2011-04-11 12:55:45 +00:00
|
|
|
|
_currentUser = value;
|
2011-01-06 07:17:00 +00:00
|
|
|
|
IsAuthenticated = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-04-11 12:55:45 +00:00
|
|
|
|
_currentUser = null;
|
2011-01-06 07:17:00 +00:00
|
|
|
|
IsAuthenticated = false;
|
2011-04-11 12:55:45 +00:00
|
|
|
|
_roles = null;
|
2014-11-06 10:39:11 +00:00
|
|
|
|
Cache.ClearRoles();
|
2011-01-06 07:17:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-16 11:11:55 +00:00
|
|
|
|
public static bool IsAllowed(string role)
|
2011-02-09 12:03:22 +00:00
|
|
|
|
{
|
2011-04-11 12:55:45 +00:00
|
|
|
|
if (_currentUser == null)
|
2011-02-09 12:03:22 +00:00
|
|
|
|
return false;
|
2011-04-11 12:55:45 +00:00
|
|
|
|
if (_roles == null)
|
|
|
|
|
_roles = new Dictionary<string, bool>();
|
2014-10-16 11:11:55 +00:00
|
|
|
|
if (!_roles.ContainsKey(role))
|
2014-11-06 10:39:11 +00:00
|
|
|
|
_roles.Add(role, Cache.UserRoles(_currentUser.UserID).Any(x => x.Name == role));
|
2014-10-16 11:11:55 +00:00
|
|
|
|
return _roles[role];
|
2011-02-09 12:03:22 +00:00
|
|
|
|
}
|
2011-01-06 07:17:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|