public SecurityHelper()
        {
            if (_db == null)
            {
                _db = SecurityDbContext.Create();
            }

            //var provider = new DpapiDataProtectionProvider("CarrotCake CMS");

            //this.UserToken = new DataProtectorTokenProvider<ApplicationUser>(provider.Create("UserToken")) {
            //	TokenLifespan = TimeSpan.FromDays(7)
            //};
        }
 public static void Register()
 {
     var cache  = MemoryCache.Default;
     var policy = new CacheItemPolicy
     {
         SlidingExpiration = new TimeSpan(1, 0, 0)
     };
     SecurityDbContext db = SecurityDbContext.Create();
     var roles            = db.Roles.ToList().Select(x => new ApplicationRole {
         Id = x.Id, Name = x.Name
     }).ToList();
     bool addedRoles = cache.Add("Roles", roles, policy);
     var  users      = db.Users.ToList().Select(
         x => new ApplicationUser
     {
         UserName = x.UserName,
         RoleName = x.RoleName,
         Id       = x.Id,
         ShopId   = x.ShopId,
         IsActive = x.IsActive
     }).ToList();
     bool addedUsers = cache.Add("Users", users, policy);
 }
        //public static AuthenticationProperties CreateProperties(string userName)
        //{
        //    IDictionary<string, string> data = new Dictionary<string, string>
        //    {
        //        { "userName", userName }
        //    };

        //    return new AuthenticationProperties(data);
        //}

        public static AuthenticationProperties CreateProperties(ApplicationUser user, IList <string> roles, Shop shop, BusinessDbContext businessDb)
        {
            IDictionary <string, string> data = new Dictionary <string, string>();

            data.Add("id", user.Id);
            data.Add("name", user.FirstName + " " + user.LastName);
            data.Add("userName", user.UserName);
            // role
            IdentityUserRole identityUserRole = user.Roles.FirstOrDefault();

            if (identityUserRole != null)
            {
                data.Add("roleId", identityUserRole.RoleId);
            }

            string roleName = roles.First();

            data.Add("role", roleName);

            data.Add("connectionId", "");

            if (string.IsNullOrWhiteSpace(user.ShopId))
            {
                user.ShopId = new Guid().ToString();
            }
            data.Add("shopId", user.ShopId);

            EmployeeInfo employeeInfo = businessDb.EmployeeInfos.FirstOrDefault(x => x.Email == user.Email && x.ShopId == user.ShopId);

            if (employeeInfo != null && !string.IsNullOrWhiteSpace(employeeInfo.WarehouseId))
            {
                data.Add("warehouseId", employeeInfo.WarehouseId);
            }

            SecurityDbContext db = SecurityDbContext.Create();
            IQueryable <ApplicationPermission> permissions = db.Permissions.Where(x => x.RoleId == identityUserRole.RoleId && x.IsAllowed);
            var resources =
                permissions.Select(x => new { name = x.Resource.Name, isAllowed = x.IsAllowed, isDisabled = x.IsDisabled })
                .ToList();
            string allowedResources = JsonConvert.SerializeObject(resources);

            data.Add("resources", allowedResources);

            var role = db.ApplicationRoles.Find(data["roleId"]);

            if (role != null)
            {
                if (string.IsNullOrWhiteSpace(role.DefaultRoute))
                {
                    role.DefaultRoute = "root.home";
                }
                data.Add("defaultRoute", role.DefaultRoute);
            }

            if (shop != null)
            {
                if (!string.IsNullOrWhiteSpace(shop.ChalanName))
                {
                    data.Add("ChalanName", shop.ChalanName);
                }

                if (!string.IsNullOrWhiteSpace(shop.ReceiptName))
                {
                    data.Add("ReceiptName", shop.ReceiptName);
                }

                data.Add("ShowOrderNumberAfterSave", shop.IsShowOrderNumber.ToString());
                data.Add("AddToCartIfResultIsOne", shop.IsAutoAddToCart.ToString());
                data.Add("DeliveryChargeAmount", shop.DeliveryCharge.ToString(CultureInfo.InvariantCulture));
            }

            return(new AuthenticationProperties(data));
        }
Exemple #4
0
 public ProfileController()
 {
     db = SecurityDbContext.Create();
 }