コード例 #1
0
        /// <summary>
        /// Gets a user using the specified ID and populates the necessary roles
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public UserModel GetUser(string email)
        {
            if (string.IsNullOrEmpty(email))
            {
                return(null);
            }

            UserModel model = new UserModel();

            model = (from u in context.Users
                     where
                     (
                         u.Email == email &&
                         u.Status == ( int )Status.Active
                     )
                     select new UserModel()
            {
                Id = u.Id,
                Pin = u.Pin,
                Cell = u.Cell,
                Name = u.Name,
                Email = u.Email,
                Surname = u.Surname,
                CreatedOn = u.CreatedOn,
                Status = ( Status )u.Status,
                RoleType = ( RoleType )u.Type,
                DisplayName = u.Name + " " + u.Surname,

                NiceCreatedOn = u.CreatedOn,
                IsAdmin = u.UserRoles.Any(ur => ur.Role.Administration),

                Roles = u.UserRoles.Select(ur => ur.Role)
                        .OrderByDescending(r => r.Id)
                        .ToList(),
                PSPs = u.PSPUsers.Select(p => p.PSP).ToList(),
                Clients = u.ClientUsers.Select(c => c.Client).ToList(),
                SelfieUrl = context.Images
                            .Where(a => a.ObjectId == u.Id && a.ObjectType == "User" && a.Name.ToLower() == "selfie")
                            .Select(s => SystemConfig.ImagesLocation + "//" + s.Location).FirstOrDefault(),
            }).FirstOrDefault();


            if (model != null)
            {
                // Get roles
                model = this.ConfigRoles(model);

                ContextExtensions.CacheUserData(model.Email, model);
            }

            return(model);
        }
コード例 #2
0
        /// <summary>
        /// Gets a user using the specified id
        /// </summary>
        /// <param name="id">Id of the user to be fetched</param>
        /// <returns></returns>
        public SimpleUserModel GetSimpleById(int id)
        {
            SimpleUserModel user = ( SimpleUserModel )ContextExtensions.GetCachedUserData("simpu_" + id);

            if (user != null)
            {
                return(user);
            }

            // Parameters
            List <object> parameters = new List <object>()
            {
                { new SqlParameter("uid", id) }
            };

            string query = "SELECT u.Id, u.Name, u.Surname, u.Email, u.IdNumber, u.Branch, u.Name + ' ' + u.Surname AS DisplayName FROM [dbo].[Users] u WHERE u.Id=@uid";

            user = context.Database.SqlQuery <SimpleUserModel>(query.Trim(), parameters.ToArray()).FirstOrDefault();

            ContextExtensions.CacheUserData("simpu_" + id, user);

            return(user);
        }
コード例 #3
0
        /// <summary>
        /// Gets a user using the specified Email Address and Password and populates necessary roles
        /// </summary>
        /// <param name="email">Email Address of the user to be fetched</param>
        /// <param name="password">Password of the user to be fetched</param>
        /// <returns></returns>
        public UserModel Login(string email, string password, bool resetPin = false)
        {
            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
            {
                return(null);
            }

            UserModel model = new UserModel();

            password = GetSha1Md5String(password);

            model = (from u in context.Users
                     where
                     (
                         u.Email.Trim() == email.Trim() &&
                         u.Password == password &&
                         u.Status == ( int )Status.Active
                     )
                     select new UserModel()
            {
                Id = u.Id,
                Pin = u.Pin,
                Cell = u.Cell,
                Name = u.Name,
                Email = u.Email,
                Surname = u.Surname,
                CreatedOn = u.CreatedOn,
                Status = ( Status )u.Status,
                RoleType = ( RoleType )u.Type,
                DisplayName = u.Name + " " + u.Surname,

                NiceCreatedOn = u.CreatedOn,
                IsAdmin = u.UserRoles.Any(ur => ur.Role.Administration),

                Roles = u.UserRoles.Select(ur => ur.Role)
                        .OrderByDescending(r => r.Id)
                        .ToList(),
                PSPs = u.PSPUsers.Select(p => p.PSP).ToList(),
                Clients = u.ClientUsers.Select(c => c.Client).ToList(),
                SelfieUrl = context.Images
                            .Where(a => a.ObjectId == u.Id && a.ObjectType == "User" && a.Name.ToLower() == "selfie")
                            .Select(s => SystemConfig.ImagesLocation + "//" + s.Location).FirstOrDefault(),
            }).FirstOrDefault();

            if (model != null)
            {
                // Get roles
                model = this.ConfigRoles(model);

                User user = context.Users.FirstOrDefault(u => u.Id == model.Id);

                if (resetPin)
                {
                    user.Pin = null;
                }

                user.LastLogin = DateTime.Now;

                context.Entry(user).State = EntityState.Modified;
                context.SaveChanges();

                ContextExtensions.CacheUserData(model.Email, model);
            }

            return(model);
        }