Exemplo n.º 1
0
        public override bool IsUserInRole(string username, string roleName)
        {
            if (username == "admin")
            {
                return(true);
            }
            SessionWrapper s = SessionManager.GetSessionWrapper();

            try
            {
                User u = OrnamentContext.DaoFactory.MemberShipFactory.CreateUserDao()
                         .GetByLoginId(username);
                IQueryable <Role> result = from role in OrnamentContext.DaoFactory.MemberShipFactory.Roles
                                           where role.Name == roleName
                                           select role;
                if (!result.Any())
                {
                    return(false);
                }
                s.Commit();
                return(u.InRole(result.First()));
            }
            finally
            {
                s.Close();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Returns the collection of settings property values for the specified application instance and settings property
        ///     group.
        /// </summary>
        /// <returns>
        ///     A <see cref="T:System.Configuration.SettingsPropertyValueCollection"></see> containing the values for the specified
        ///     settings property group.
        /// </returns>
        /// <param name="context">
        ///     A <see cref="T:System.Configuration.SettingsContext"></see> describing the current application use.
        /// </param>
        /// <param name="collection">
        ///     A <see cref="T:System.Configuration.SettingsPropertyCollection"></see> containing the settings property group whose
        ///     values are to be retrieved.
        /// </param>
        /// <filterpriority>2</filterpriority>
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context,
                                                                          SettingsPropertyCollection collection)
        {
            SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper();

            IUserProfileDao profileDao = MemberShipFactory.CreateProfileDao();

            try
            {
                var result = new SettingsPropertyValueCollection();
                Dictionary <string, object> persisteProfileValue = null;
                string       userName     = LoginId(context);
                ProfileValue profileValue = profileDao.FindByLoginId(userName);
                if (profileValue != null)
                {
                    persisteProfileValue = profileValue.Properities;
                }
                foreach (SettingsProperty property in collection)
                {
                    var item = new SettingsPropertyValue(property);
                    if (persisteProfileValue != null && persisteProfileValue.ContainsKey(item.Name))
                    {
                        item.PropertyValue = persisteProfileValue[item.Name];
                    }
                    result.Add(item);
                }
                sessionWrapper.Commit();
                return(result);
            }
            finally
            {
                sessionWrapper.Close();
            }
        }
        public virtual void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper();

            try
            {
                if (AuthorizeCore(filterContext.HttpContext))
                {
                    // ** IMPORTANT **
                    // Since we're performing authorization at the action level, the authorization code runs
                    // after the output caching module. In the worst case this could allow an authorized user
                    // to cause the page to be cached, then an unauthorized user would later be served the
                    // cached page. We work around this by telling proxies not to cache the sensitive page,
                    // then we hook our custom authorization code into the caching mechanism so that we have
                    // the final say on whether a page should be served from the cache.

                    HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
                    cachePolicy.SetProxyMaxAge(new TimeSpan(0));
                    cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */);
                }
                else
                {
                    // auth failed, redirect to login page
                    filterContext.Result = new HttpUnauthorizedResult();
                }

                sessionWrapper.Commit();
            }
            finally
            {
                sessionWrapper.Close();
            }
        }
Exemplo n.º 4
0
        private void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs args)
        {
            SessionWrapper wrapper = SessionManager.GetSessionWrapper();

            try
            {
                IUserProfileDao profileDao = OrnamentContext.DaoFactory.MemberShipFactory.CreateProfileDao();
                ProfileValue    anonymous  = profileDao.FindByLoginId(args.AnonymousID);
                if (anonymous != null)
                {
                    //合并anonymous profile
                    ProfileBase currenProfile = HttpContext.Current.Profile;
                    foreach (string key in anonymous.Properities.Keys)
                    {
                        currenProfile.SetPropertyValue(key, anonymous.Properities[key]);
                    }
                    profileDao.Delete(anonymous);
                    currenProfile.Save();
                    AnonymousIdentificationModule.ClearAnonymousIdentifier();
                }


                //最后,一更新Multi-lang的cookie,因此使用Profile的语言。
                OrnamentContext.MemberShip.SwitchLanguage(OrnamentContext.MemberShip.CurrentUser().GetLanguage());
                wrapper.Commit();
            }
            catch (Exception ex)
            {
                ILog log = LogManager.GetLogger(typeof(GlobalContext));
                log.Error(ex.Message, ex);
            }
            finally
            {
                wrapper.Close();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Sets the values of the specified group of property settings.
        /// </summary>
        /// <param name="context">
        ///     A <see cref="T:System.Configuration.SettingsContext"></see> describing the current application usage.
        /// </param>
        /// <param name="collection">
        ///     A <see cref="T:System.Configuration.SettingsPropertyValueCollection"></see> representing the group of property
        ///     settings to set.
        /// </param>
        /// <filterpriority>2</filterpriority>
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper();

            try
            {
                string          userName   = LoginId(context);
                IUserProfileDao profileDao = MemberShipFactory.CreateProfileDao();


                ProfileValue profileValue = profileDao.FindByLoginId(userName) ??
                                            new ProfileValue
                {
                    LastActivityDate = DateTime.Now,
                    IsAnonymous      = !userIsAuthenticated(context),
                    LoginId          = userName
                };
                foreach (SettingsPropertyValue settingsPropertyValue in collection)
                {
                    if (profileValue.Properities.ContainsKey(settingsPropertyValue.Name))
                    {
                        profileValue.Properities[settingsPropertyValue.Name] = settingsPropertyValue.PropertyValue;
                    }
                    else
                    {
                        profileValue.Properities.Add(settingsPropertyValue.Name, settingsPropertyValue.PropertyValue);
                    }
                }
                profileDao.SaveOrUpdate(profileValue);
                sessionWrapper.Commit();
            }
            finally
            {
                sessionWrapper.Close();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        ///     When overridden in a derived class, retrieves profile information for profiles in which
        ///     the last activity date occurred on or before the specified date and the user name matches the specified user name.
        /// </summary>
        /// <returns>
        ///     A <see cref="T:System.Web.Profile.ProfileInfoCollection"></see> containing user profile information for inactive
        ///     profiles where the user name matches the supplied usernameToMatch parameter.
        /// </returns>
        /// <param name="authenticationOption">
        ///     One of the <see cref="T:System.Web.Profile.ProfileAuthenticationOption"></see> values, specifying whether
        ///     anonymous, authenticated, or both types of profiles are returned.
        /// </param>
        /// <param name="userInactiveSinceDate">
        ///     A <see cref="T:System.DateTime"></see> that identifies which user profiles are considered inactive. If the
        ///     <see
        ///         cref="P:System.Web.Profile.ProfileInfo.LastActivityDate">
        ///     </see>
        ///     value of a user profile occurs on or before this date and time, the profile is considered inactive.
        /// </param>
        /// <param name="totalRecords">When this method returns, contains the total number of profiles.</param>
        /// <param name="pageIndex">The index of the page of results to return.</param>
        /// <param name="usernameToMatch">The user name to search for.</param>
        /// <param name="pageSize">The size of the page of results to return.</param>
        public override ProfileInfoCollection FindInactiveProfilesByUserName(
            ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate,
            int pageIndex, int pageSize, out int totalRecords)
        {
            SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper();

            try
            {
                var infos = new ProfileInfoCollection();


                IQueryable <ProfileValue> profiles;
                switch (authenticationOption)
                {
                case ProfileAuthenticationOption.All:
                    profiles = (from pf in MemberShipFactory.Profiles
                                where
                                pf.LastActivityDate < userInactiveSinceDate &&
                                pf.LoginId.StartsWith(usernameToMatch)
                                select pf);

                    totalRecords = (from pf in MemberShipFactory.Profiles
                                    where
                                    pf.LastActivityDate < userInactiveSinceDate &&
                                    pf.LoginId.StartsWith(usernameToMatch)
                                    select pf).Count();
                    break;

                case ProfileAuthenticationOption.Anonymous:
                    profiles = (from pf in MemberShipFactory.Profiles
                                where
                                pf.LastActivityDate < userInactiveSinceDate &&
                                pf.LoginId.StartsWith(usernameToMatch) && pf.IsAnonymous
                                select pf);
                    totalRecords = (from pf in MemberShipFactory.Profiles
                                    where
                                    pf.LastActivityDate < userInactiveSinceDate &&
                                    pf.LoginId.StartsWith(usernameToMatch) && pf.IsAnonymous
                                    select pf).Count();
                    break;

                default:
                    profiles = (from pf in MemberShipFactory.Profiles
                                where
                                pf.LastActivityDate < userInactiveSinceDate &&
                                pf.LoginId.StartsWith(usernameToMatch) && pf.IsAnonymous == false
                                select pf);
                    totalRecords = (from pf in MemberShipFactory.Profiles
                                    where
                                    pf.LastActivityDate < userInactiveSinceDate &&
                                    pf.LoginId.StartsWith(usernameToMatch) && pf.IsAnonymous == false
                                    select pf).Count();
                    break;
                }


                foreach (ProfileValue prof in profiles)
                {
                    User u = MemberShipFactory.CreateUserDao().GetByLoginId(prof.LoginId);
                    infos.Add(new ProfileInfo(u.Name, prof.IsAnonymous, u.Other.LastActivityDate.Value,
                                              prof.LastActivityDate.Value, 30));
                }
                sessionWrapper.Commit();
                return(infos);
            }
            finally
            {
                sessionWrapper.Close();
            }
        }