Exemplo n.º 1
0
        public static DataSourceResult ToDataSourceResult <T, Td>(this IGenericUnitOfWork generalRepository, string queryable, KendoDataRequest kendoDataRequest,
                                                                  Sort initialsort, QueryType queryType = QueryType.None)
        {
            int total;

            if (queryType == QueryType.NeedToWrapBySelect)
            {
                queryable = $"select * from ({queryable}) wrapped";
            }
            string filteredQuery = GetQueryable(queryable, generalRepository, kendoDataRequest, out total, queryType == QueryType.AlreadyHasWhereClause);

            queryable = Page(filteredQuery, kendoDataRequest.Take, kendoDataRequest.Skip, kendoDataRequest.Sort, initialsort, kendoDataRequest.DataExtensions);
            var data   = generalRepository.SqlQuery <T>(queryable).ToList();
            var mapper = new DiObjectMapper();
            var mapped = mapper.Map <IList <T>, IList <Td> >(data.ToList());

            return(new DataSourceResult()
            {
                Data = mapped,
                Total = total
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Applies data processing (paging, sorting and filtering) over IQueryable using Dynamic Linq.
        /// </summary>
        /// <typeparam name="T">The source type of the IQueryable</typeparam>
        /// <typeparam name="TD">The destination type of the IQueryable</typeparam>
        /// <param name="queryable">The IQueryable which should be processed.</param>
        /// <param name="kendoDataRequest"></param>
        /// <param name="initialsort"></param>
        /// <returns>A DataSourceResult object populated from the processed IQueryable.</returns>
        public static DataSourceResult ToDataSourceResult <T, TD>(this IQueryable <T> queryable, KendoDataRequest kendoDataRequest, Sort initialsort)
        {
            var mapper = new DiObjectMapper();


            // Filter the data first
            queryable = Filter(queryable, kendoDataRequest.Filter, kendoDataRequest.DataExtensions);

            // Calculate the total number of records (needed for paging)
            var total = queryable.Count();

            // Sort the data
            queryable = Sort(queryable, kendoDataRequest.Sort, initialsort, kendoDataRequest.DataExtensions);

            // Finally page the data
            queryable = Page(queryable, kendoDataRequest.Take, kendoDataRequest.Skip);
            var mapped = mapper.Map <IList <T>, IList <TD> >(queryable.ToList());

            return(new DataSourceResult
            {
                Data = mapped,
                Total = total
            });
        }
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            //var allowedOrigin = "*";
            //context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new { allowedOrigin });
            var  userManager = context.OwinContext.GetUserManager <ApplicationUserManager>();
            User user        = new User();

            user = await userManager.FindAsync(context.UserName, context.Password);

            var mapper = new DiObjectMapper();
            ApplicationLogViewModel logViewModel = new ApplicationLogViewModel()
            {
                Action = "Login",
                Data   = user,
                Entity = "User"
            };

            if (user == null)
            {
                logViewModel.Data = new { UserName = context.UserName }
            }
            ;

            var appLog = mapper.Map <ApplicationLogViewModel, ApplicationLog>(logViewModel);

            appLog.IpAddress = GeneralService.ClientIpFromHeader;
            appLog.LogType   = General.Enums.LogType.System;
            appLog.Date      = GeneralService.CurrentDate;

            var mbosContext = MBOSContext.Create();

            if (user == null)
            {
                appLog.Description = "Invalid username or password";
                mbosContext.ApplicationLogs.Add(appLog);
                mbosContext.SaveChanges();
                context.SetError("invalid_grant", "The username or password is incorrect.");
                return;
            }
            if (!user.EmailConfirmed)
            {
                appLog.Description = "Account email is not confirmed.";
                mbosContext.ApplicationLogs.Add(appLog);
                mbosContext.SaveChanges();
                context.SetError("invalid_grant", "Email is not confirmed yet.");
                return;
            }
            if (user.AccessFailedCount >= 3)
            {
                appLog.Description = "Account is locked.";
                mbosContext.ApplicationLogs.Add(appLog);
                mbosContext.SaveChanges();
                context.SetError("invalid_grant", "Your account has been locked. Please contact the administrator!");
                return;
            }
            appLog.UserId      = user.Id;
            appLog.Description = "Login successful.";
            mbosContext.ApplicationLogs.Add(appLog);
            mbosContext.SaveChanges();
            var loginInfo = new LoginInfo()
            {
                UserId            = user.Id,
                FullName          = user.FullName,
                DisplayPicture    = string.IsNullOrEmpty(user.DisplayPicture) ? "Resources/DisplayPictures/noimage.jpg" : user.DisplayPicture,
                LoginUserBranches = user.UserBranches.Select(c => new LoginUserBranch()
                {
                    Id = (Guid)c.BranchId, Name = c.Branch.Name, ShortName = c.Branch.ShortName
                }).ToList(),
                RoleId       = user.Roles.Select(c => c.RoleId).FirstOrDefault(),
                RoleType     = user.Roles.Select(c => c.Role.RoleType).FirstOrDefault(),
                DepartmentId = user.DepartmentId, Role = user.Roles.Select(c => c.Role.Name).FirstOrDefault()
            };
            var loginInfoValue = Newtonsoft.Json.JsonConvert.SerializeObject(loginInfo);
            IDictionary <string, string> authProp = new Dictionary <string, string>
            {
                { "LoginInfo", loginInfoValue }
            };
            var claims = new List <Claim>()
            {
                new Claim("LoginInfo", loginInfoValue)
            };
            ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType, claims);

            ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager, CookieAuthenticationDefaults.AuthenticationType, claims);


            AuthenticationProperties properties = new AuthenticationProperties(authProp);
            AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }