public PermissionBasedPrincipal(IIdentity identity, UserInfo userInfo)
        {
            if (identity == null)
                throw new ArgumentNullException("identity");
            if (userInfo == null)
                throw new ArgumentNullException("userInfo");

            Identity = identity;
            UserInfo = userInfo;
        }
Esempio n. 2
0
        /// <summary>
        /// Get cached principal. Create new one and insert to cache if not existed
        /// </summary>
        public static PermissionBasedPrincipal GetPrincipal(string username)
        {
            // TODO: consider allowing to call this method for current user only!
            string key = GetPrincipalKey(username);

            // Get cached principal
            object data = Cacher.GetData(key);

            if (data == null) // If not existed create new one and insert to cache
            {
                lock (_lockObject) // Double-checked locking
                {
                    data = Cacher.GetData(key);
                    if (data == null)
                    {
                        ISecurityRepository repository = IoC.UnityContainer.Resolve<ISecurityRepository>();
                        Feedback<User> feedback = repository.GetBasicUserInfo(username);
                        if (feedback.Success)
                        {
                            IList<string> roles = new List<string>();
                            foreach (var userInRole in feedback.Data.UserRoles)
                            {
                                var role = ReferenceDataCache.RoleCollection.FirstOrDefault(i => i.Id == userInRole.RoleId);
                                if (role != null)
                                    roles.Add(role.Name);
                            }
                            UserInfo info = new UserInfo
                            {
                                Id = feedback.Data.Id,
                                Email = feedback.Data.Email,
                                Username = username,
                                AvatarUrl = feedback.Data.AvatarUrl,
                                Point = feedback.Data.Point,
                                Roles = roles
                            };
                            data = new PermissionBasedPrincipal(new PermissionBasedIdentity(username), info);
                            Cacher.AddToCache(key, data, CacheItemPriority.High,
                                CacheHelper.GetAbsoluteExpirationFromConfig(Constants.ConfigKeys.Caching.PrincipalCacheMinutes));
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
            }

            return (PermissionBasedPrincipal)data;
        }