Exemple #1
0
        private static void GetCurrentResource(this HttpContext context, IConfiguration configuration, ref List <CPResourceEntity> currentResource, ref CPLangEntity currentLang)
        {
            string cookie = context.GetValue(Cookies.DefaultLang, false);

            if (string.IsNullOrEmpty(cookie))
            {
                cookie = "vn"; context.SetCurrentLang("VN");
            }
            var lang = new CPLangService(configuration);

            currentLang = lang.CreateQuery().Find(o => o.IsActive == true && o.Code == cookie)?.SingleOrDefault();
            if (currentLang != null)
            {
                // cache
                var cache = CacheExtends.GetDataFromCache <List <CPResourceEntity> >(cookie + "-" + CacheExtends.DefaultLang);
                //
                if (cache != null)
                {
                    currentResource = cache; return;
                }

                var res = new CPResourceService(configuration);
                currentResource = res.GetByLangID(currentLang.ID);
            }
        }
        //<div class="mdc-select">
        //    <i class="mdc-select__dropdown-icon"></i>
        //    <select class="mdc-select__native-control">
        //        <option value = "" disabled selected></option>
        //        <option value = "grains" >
        //            Bread, Cereal, Rice, and Pasta
        //        </option>
        //        <option value = "vegetables" >
        //            Vegetables
        //        </ option >
        //        < option value="fruit">
        //            Fruit
        //        </option>
        //    </select>
        //    <label class="mdc-floating-label">Pick a Food Group</label>
        //    <div class="mdc-line-ripple"></div>
        //</div>
        public static IHtmlContent GetMenuForWeb(string type, string menuID, string langID)
        {
            string keyCache = type + "_ChuyenMuc_" + langID;
            var    cache    = CacheExtends.GetDataFromCache <IHtmlContent>(keyCache);

            if (cache != null)
            {
                return(cache);
            }
            else
            {
                var    chuyenmuc = Instance.CreateInstanceCPMenu("CPMenus");
                var    data      = chuyenmuc.GetItemByType(type, langID);
                string Html      = "<div class=\"mdc-select\"><i class=\"mdc-select__dropdown-icon\"></i><select name=\"MenuID\" class=\"mdc-select__native-control\"><option value=\"\">------ Chọn chuyên mục -----</option>";
                for (int i = 0; data != null && i < data.Count; i++)
                {
                    var item = data[i];
                    if (menuID == item.ID)
                    {
                        Html += "<option value =" + item.ID + " selected>" + item.Name + "</option>";
                    }
                    else
                    {
                        Html += "<option value =" + item.ID + ">" + item.Name + "</option>";
                    }
                }
                Html += "</select></div>";
                return(new HtmlString(Html));
            }
        }
Exemple #3
0
        protected List <MenuControlAttribute> GetAdminContrller()
        {
            var cacheMenu = CacheExtends.GetDataFromCache <List <MenuControlAttribute> >(CacheExtends.DefaultAdminController);

            if (cacheMenu != null)
            {
                return(cacheMenu);
            }
            else
            {
                var cusMenus = new List <MenuControlAttribute>();
                var assembly = Assembly.GetAssembly(typeof(AdminController)).GetTypes()
                               .Where(o => o.BaseType.FullName == typeof(AdminController).FullName)
                               .ToList();
                int count = assembly != null ? assembly.Count : 0;
                for (int i = 0; i < count; i++)
                {
                    var item      = assembly[i];
                    var attribute = item.GetCustomAttribute <MenuControlAttribute>();
                    if (attribute == null)
                    {
                        continue;
                    }
                    var getmetho = item.GetMethods();
                    var method   = getmetho
                                   .Where(o => o.Module.Name == typeof(AdminController).Module.Name)
                                   .Select(o => o.Name).ToList();
                    if (method != null)
                    {
                        foreach (var m in method)
                        {
                            attribute.ActionName.Add(m.ToLower());
                        }
                    }
                    attribute.Color = string.IsNullOrEmpty(attribute.Color) ? new GetColor().RandomColor() : attribute.Color;
                    cusMenus.Add(attribute);
                }
                CacheExtends.SetObjectFromCache(CacheExtends.DefaultAdminController, 360 * 24 * 60, cusMenus);

                return(cusMenus);
            }
        }
        public string RandomColor()
        {
            var _color = new List <string>();
            var cacle  = CacheExtends.GetDataFromCache <List <string> >("colorGet");

            if (_color.Count == 0)
            {
                _color = cacle != null && cacle.Count > 0? cacle : new List <string>()
                {
                    "cyan", "red", "emerald", "indigo", "violet", "teal"
                };
            }
            Random random      = new Random();
            int    id          = random.Next(0, _color.Count);
            var    colorReturn = _color[id].ToString();

            _color.Remove(colorReturn);
            CacheExtends.SetObjectFromCache("colorGet", 240, _color);
            return(colorReturn);
        }
        /// <summary>
        /// Render html partialView with data from db
        /// </summary>
        /// <param name="partialView"></param
        /// <param name="ctrl"></param>
        /// <param name="properties"> key | value \r\n key | value \r\n  key | value</param>
        /// <returns></returns>

        public static IHtmlContent RenderWithData(this IHtmlHelper htmlHelper, string partialView, string ctrl, List <SysTemplatePropertyEntity> properties)
        {
            string keyCache = ctrl + "_partialView_" + partialView;
            var    cache    = CacheExtends.GetDataFromCache <IHtmlContent>(keyCache);

            if (cache == null)
            {
                var customer = properties;

                var data = GetDynamicDataByActName(ctrl, customer);
                var html = htmlHelper.PartialAsync(partialView, data);

                CacheExtends.SetObjectFromCache(keyCache, 10 * 60 * 24, html.Result);

                return(html.Result);
            }
            else
            {
                return(cache);
            }
        }
Exemple #6
0
        protected void GetClientContrller(ref List <MenuControlAttribute> cusMenus, ref List <CModule> modules)
        {
            var cacheControl = CacheExtends.GetDataFromCache <List <CModule> >(CacheExtends.DefaultIsControl);
            var cacheMenu    = CacheExtends.GetDataFromCache <List <MenuControlAttribute> >(CacheExtends.DefaultClientController);

            if (cacheMenu != null && cacheControl != null)
            {
                cusMenus = cacheMenu;
                modules  = cacheControl;
            }
            else
            {
                modules  = new List <CModule>();
                cusMenus = new List <MenuControlAttribute>();
                var assembly = Assembly.GetAssembly(typeof(ClientController)).GetTypes()
                               .Where(o => o.BaseType.FullName == typeof(ClientController).FullName)
                               .ToList();
                int count = assembly != null ? assembly.Count : 0;
                for (int i = 0; i < count; i++)
                {
                    var item      = assembly[i];
                    var attribute = item.GetCustomAttribute <MenuControlAttribute>();
                    if (attribute == null)
                    {
                        continue;
                    }
                    if (attribute.IsControl)
                    {
                        var fields = item.GetFields();
                        List <ProperyCModule> pp = new List <ProperyCModule>();
                        if (fields != null)
                        {
                            foreach (var field in fields)
                            {
                                var    attr = field.GetCustomAttribute <PropertyAttribute>();
                                string name = attr.Name;
                                string key  = field.Name;
                                string type = attr.Type;
                                pp.Add(new ProperyCModule(key, type, name));
                            }
                        }
                        modules.Add(new CModule()
                        {
                            Code       = attribute.CModule,
                            Name       = attribute.Name,
                            FullName   = item.FullName,
                            Properties = pp
                        });
                    }
                    else
                    {
                        var getmetho = item.GetMethods();
                        var method   = getmetho
                                       .Where(o => o.Module.Name == typeof(ClientController).Module.Name)
                                       .Select(o => o.Name).ToList();
                        if (method != null)
                        {
                            foreach (var m in method)
                            {
                                attribute.ActionName.Add(m.ToLower());
                            }
                        }
                        cusMenus.Add(attribute);
                    }
                }
                CacheExtends.SetObjectFromCache(CacheExtends.DefaultIsControl, 360 * 24 * 60, modules);
                CacheExtends.SetObjectFromCache(CacheExtends.DefaultClientController, 360 * 24 * 60, cusMenus);
            }
        }
Exemple #7
0
        private static ClaimsPrincipal GetCurrentUser(this HttpContext context, IConfiguration configuration)
        {
            string token = context.GetValue(Cookies.DefaultLogin, false);

            if (string.IsNullOrEmpty(token))
            {
                return(null);
            }
            else
            {
                // neeus co cache
                var cache = CacheExtends.GetDataFromCache <ClaimsPrincipal>(token);
                if (cache != null)
                {
                    return(cache);
                }
                // ko co cache
                var    logs  = new CPLoginLogService(configuration);
                string email = logs.GetEmailFromDb(token);
                if (string.IsNullOrEmpty(email))
                {
                    return(null);
                }
                else
                {
                    var account = new CPUserService(configuration);
                    var user    = account.GetItemByEmail(email);
                    if (user == null)
                    {
                        return(null);
                    }
                    else
                    {
                        var role  = new CPRoleService(configuration);
                        var irole = role.GetByID(user.RoleID);
                        if (role == null)
                        {
                            return(null);
                        }
                        var claims = new List <Claim>
                        {
                            new Claim(ClaimTypes.Email, user.Email),
                            new Claim(ClaimTypes.Name, user.Name),
                            new Claim(ClaimTypes.Role, irole.Code),
                            new Claim("RoleID", irole.ID.ToString()),
                            new Claim("UserID", user.ID)
                        };
                        var claimsIdentity = new ClaimsIdentity(claims, Cookies.DefaultLogin);
                        _ = new AuthenticationProperties
                        {
                            IsPersistent = true,
                            ExpiresUtc   = DateTime.UtcNow.AddMinutes(Cookies.ExpiresLogin)
                        };
                        ClaimsPrincipal claim = new ClaimsPrincipal();
                        claim.AddIdentity(claimsIdentity);

                        CacheExtends.SetObjectFromCache(token, Cookies.ExpiresLogin, claim);

                        return(claim);
                    }
                }
            }
        }