Example #1
0
        public static bool SwitchLanguage(this MemberShipContext context, CultureInfo language)
        {
            if (language == null)
            {
                throw new ArgumentNullException("language");
            }

            Thread.CurrentThread.CurrentCulture   = language;
            Thread.CurrentThread.CurrentUICulture = language;


            HttpContext.Current.Response.Cookies.Add(new HttpCookie(LangCookieName)
            {
                Value    = language.Name,
                HttpOnly = false,
            });

            User currentUser = OrnamentContext.MemberShip.CurrentUser();

            if (currentUser != null)
            {
                if (language.Name != currentUser.GetLanguage().Name)
                {
                    currentUser.Language = language.Name;
                    OrnamentContext.DaoFactory.MemberShipFactory.CreateUserDao().SaveOrUpdate(currentUser);
                }
            }
            return(true);
        }
Example #2
0
        /// <summary>
        /// </summary>
        public static User CurrentUser(this MemberShipContext context)
        {
            if (HttpContext.Current == null || HttpContext.Current.User == null ||
                !HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return(null);
            }
            IUserDao a = OrnamentContext.DaoFactory.MemberShipFactory.CreateUserDao();

            User user = a.GetByLoginId(HttpContext.Current.User.Identity.Name);

            if (user == null)
            {
                FormsAuthentication.SignOut();
                FormsAuthentication.RedirectToLoginPage();
                return(null);
            }
            //如果最后一次访问大于设置值,那么需要更新一下LastActivitiyDate的值。
            DateTime now = DateTime.Now;

            if (user.Other.LastActivityDate == null ||
                (now - user.Other.LastActivityDate.Value).Minutes >= Membership.UserIsOnlineTimeWindow / 3)
            {
                user.Other.LastActivityDate = now;
                a.SaveOrUpdate(user);
                a.Flush();
            }
            return(user);
        }
 /// <summary>
 /// </summary>
 /// <param name="ornamentContext"></param>
 /// <param name="resourceType"></param>
 /// <param name="resourceId"></param>
 /// <returns></returns>
 public static object GetResource(this MemberShipContext ornamentContext, Type resourceType, object resourceId)
 {
     if (resourceType == typeof(string))
     {
         return(resourceId);
     }
     return(OrnamentContext.DaoFactory.GetDaoFactory <IMemberShipFactory>().CreateResourceDao().Get(resourceType, resourceId));
 }
Example #4
0
        /// <summary>
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static int OffSetHour(this MemberShipContext context)
        {
            int?clientSetting = OrnamentModule.GetOffSetHour();

            if (clientSetting == null)
            {
                return(0);
            }
            return(clientSetting.Value);
        }
 ///
 public SiteMapPermission(MemberShipContext memberShipContext, ResourceManager manager)
 {
     if (memberShipContext == null)
     {
         throw new ArgumentNullException("memberShipContext");
     }
     if (manager == null)
     {
         throw new ArgumentNullException("manager");
     }
     _memberShipContext = memberShipContext;
 }
Example #6
0
        public static CultureInfo CurrentLanguage(this MemberShipContext context)
        {
            CultureInfo lang = CookieRequestLanguage(context);

            if (lang == null)
            {
                User user = CurrentUser(context);
                lang = user != null?user.GetLanguage() : BroswerLanguage(context);
            }

            return(lang ?? CultureInfo.GetCultureInfo("en"));
        }
Example #7
0
        /// <summary>
        /// </summary>
        /// <param name="context"></param>
        /// <param name="serverTime"></param>
        /// <returns></returns>
        public static DateTime ToClientDateTime(this MemberShipContext context, DateTime serverTime)
        {
            User user = context.CurrentUser();

            if (user != null)
            {
                string timeZoneId = user.TimeZoneId;
                if (!String.IsNullOrEmpty(timeZoneId))
                {
                    return(TimeZoneInfo.ConvertTimeBySystemTimeZoneId(serverTime, timeZoneId));
                }
            }
            return(serverTime.AddHours(context.OffSetHour()));
        }
Example #8
0
        public static CultureInfo CookieRequestLanguage(this MemberShipContext context)
        {
            HttpCookie request = HttpContext.Current.Request.Cookies[LangCookieName];

            if (request != null)
            {
                try
                {
                    return(CultureInfo.GetCultureInfo(request.Value));
                }
                catch (Exception)
                {
                    return(null);
                }
            }
            return(null);
        }
Example #9
0
 /// <summary>
 /// 获取当前浏览器的默认语言,如果和网站支持语言一致,那么返回网站第一个支持的语言
 /// 否知返回线程语言,一般是英语
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public static CultureInfo BroswerLanguage(this MemberShipContext context)
 {
     if (HttpContext.Current != null && HttpContext.Current.Request.UserLanguages != null)
     {
         foreach (string lang in HttpContext.Current.Request.UserLanguages)
         {
             try
             {
                 var culture = CultureInfo.GetCultureInfo(lang);
                 if (OrnamentContext.Configuration.Languages.Any(language => language.CultureInfo.Name.ToLower() == culture.Name.ToLower()))
                 {
                     return(culture);
                 }
             }
             catch (CultureNotFoundException)
             {
             }
         }
     }
     return(CultureInfo.GetCultureInfo("en"));
 }
Example #10
0
        public MenuPermission(string express, MemberShipContext context)
        {
            _express = express;
            _context = context;

            string[] ary = _express.Split(':');
            if (ary.Length < 0)
            {
                throw new FormatException("expression is not a right format, it should be [resource]:[operator]");
            }
            Resource = ary[0];
            try
            {
                Type enmType = OrnamentContext.ResourceManager.GetOperator(ary[0]);
                OperatorValue = (Enum)Enum.Parse(enmType, ary[1], true);
            }
            catch (ArgumentException)
            {
                throw new FormatException(String.Format("{0} is not recognized in the {1}", ary[1],
                                                        OrnamentContext.ResourceManager.GetOperator(ary[0]).Name));
            }
        }
 /// <summary>
 /// </summary>
 /// <param name="ornamentContext"></param>
 /// <param name="resourceType"></param>
 /// <param name="resourceId"></param>
 /// <returns></returns>
 public static object GetResource(this MemberShipContext ornamentContext, Type resourceType, string resourceId)
 {
     return(resourceType == typeof(string)
                ? resourceId
                : OrnamentContext.DaoFactory.MemberShipFactory.CreateResourceDao().Get(resourceType, resourceId));
 }
Example #12
0
 public static string CurrentVerifyCode(this MemberShipContext context)
 {
     return(HttpContext.Current.Session[VerifyCodeKey] as string);
 }
Example #13
0
        public static int GetUtcOffset(this MemberShipContext ocontext)
        {
            int?utcOffset = OrnamentModule.GetOffSetHour();

            return(utcOffset.HasValue ? utcOffset.GetValueOrDefault() : Convert.ToInt32(TimeZoneInfo.Local.BaseUtcOffset.TotalHours));
        }
Example #14
0
 /// <summary>
 /// </summary>
 /// <param name="context"></param>
 /// <param name="resType"></param>
 /// <param name="id"></param>
 /// <param name="resOperator"></param>
 /// <returns></returns>
 public static bool HasRight(this MemberShipContext context, Type resType, object id, Enum resOperator)
 {
     return(context.HasRight(context.CurrentUser(), resType, id, resOperator));
 }
Example #15
0
 /// <summary>
 ///     把客户端时间转换为服务器时间
 /// </summary>
 /// <param name="context"></param>
 /// <param name="clientTime"></param>
 /// <returns></returns>
 public static DateTime ToServerDateTime(this MemberShipContext context, DateTime clientTime)
 {
     return(clientTime.AddHours(-context.OffSetHour()));
 }
Example #16
0
 /// <summary>
 /// </summary>
 /// <param name="context"></param>
 /// <param name="resource"></param>
 /// <param name="resOperator"></param>
 /// <returns></returns>
 public static bool HasRight(this MemberShipContext context, object resource, Enum resOperator)
 {
     return(context.HasRight(context.CurrentUser(), resource, resOperator));
 }
 private UserContextInner()
 {
     _memberShip = new MemberShipContext(DaoFactory.MemberShipFactory);
 }