예제 #1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Retrieves the user from session </summary>
        /// <remarks>   Andre Beging, 03.05.2018. </remarks>
        /// <param name="httpContext">  The httpContext to act on. </param>
        /// <returns>   The user. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static User GetUser(this HttpContext httpContext)
        {
            // Get User from session
            var sessionUser = httpContext?.Session?.GetObject <User>("User");

            if (sessionUser == null)
            {
                return(null);
            }

            // Check if user needs to be updated
            if (!SharedProperties.OutdatedObjects.Contains(sessionUser.UserId))
            {
                return(sessionUser);
            }

            // If so, update session
            using (var context = ContextHelper.OpenContext())
            {
                SharedProperties.OutdatedObjects.RemoveAll(x => x.Equals(sessionUser.UserId));

                var contextUser = context.User.FirstOrDefault(u => u.UserId == sessionUser.UserId);
                if (contextUser == null)
                {
                    return(sessionUser);
                }

                httpContext.Session.SetObject("User", contextUser);
            }

            return(httpContext.Session?.GetObject <User>("User"));
        }
예제 #2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Generates a new event code </summary>
        /// <remarks>   Andre Beging, 27.04.2018. </remarks>
        /// <returns>   The code. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static string GenerateCode()
        {
            var code = AuthenticationHelper.GenerateSalt().Substring(0, 4).ToLower();

            using (var context = ContextHelper.OpenContext())
            {
                if (context.Event.Any(e => e.Code.ToLower() == code))
                {
                    return(GenerateCode());
                }
            }

            return(code);
        }
예제 #3
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Is valid. </summary>
        /// <remarks>   Andre Beging, 03.05.2018. </remarks>
        /// <param name="value">                The value. </param>
        /// <param name="validationContext">    Context for the validation. </param>
        /// <returns>   A ValidationResult. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!(value is string mailAdress))
            {
                return(null);
            }

            using (var context = ContextHelper.OpenContext())
            {
                if (context.User.Any(u => u.Mail.ToLower() == mailAdress.ToLower()))
                {
                    return(new ValidationResult(FormatErrorMessage(validationContext.DisplayName)));
                }
            }

            return(null);
        }
예제 #4
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Builds a javascript compatible list of both known origins from <see cref="SharedProperties.KnownOrigins" />
        ///     and origins from the database
        /// </summary>
        /// <remarks>   Andre Beging, 03.05.2018. </remarks>
        /// <returns>   A HtmlString. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static HtmlString BuildTypeaheadOrigins()
        {
            var outputOrigins = new List <string>(SharedProperties.KnownOrigins);

            using (var context = ContextHelper.OpenContext())
            {
                var databaseOrigins = context.User.Select(u => u.Origin).ToList();

                foreach (var databaseOrigin in databaseOrigins)
                {
                    if (outputOrigins.All(o => o.Trim().ToLower() != databaseOrigin.Trim().ToLower()))
                    {
                        outputOrigins.Add(databaseOrigin.Trim());
                    }
                }
            }

            var joinedNames = outputOrigins.Select(x => string.Format("'{0}'", x)).Aggregate((a, b) => a + ", " + b);

            return(new HtmlString(joinedNames));
        }
예제 #5
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Builds a javascript compatible list of both known origins from <see cref="SharedProperties.KnownOrigins" />
        ///     and origins from the database
        /// </summary>
        /// <remarks>   Andre Beging, 03.05.2018. </remarks>
        /// <returns>   A HtmlString. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static HtmlString BuildTypeaheadOrigins()
        {
            var outputOrigins = new List <string>(SharedProperties.KnownOrigins);

            using (var context = ContextHelper.OpenContext())
            {
                var databaseOrigins = context.User.Select(u => u.Origin).Where(x => !string.IsNullOrWhiteSpace(x));

                foreach (var databaseOrigin in databaseOrigins)
                {
                    if (!outputOrigins.Any(oo =>
                                           string.Equals(oo.Trim(), databaseOrigin.Trim(), StringComparison.InvariantCultureIgnoreCase)))
                    {
                        outputOrigins.Add(databaseOrigin.Trim());
                    }
                }
            }

            var joinedNames = outputOrigins.Select(x => string.Format("'{0}'", x)).Aggregate((a, b) => a + ", " + b);

            return(new HtmlString(joinedNames));
        }
예제 #6
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Validates the login data </summary>
        /// <remarks>   Andre Beging, 26.04.2018. </remarks>
        /// <param name="mail">     The mail. </param>
        /// <param name="password"> The password. </param>
        /// <param name="user">The matching user</param>
        /// <returns>   True if it succeeds, false if it fails. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static bool LoginCorrect(string mail, string password, out User user)
        {
            user = null;

            using (var context = ContextHelper.OpenContext())
            {
                var contextUser = context.User.FirstOrDefault(u => u.Mail.ToLower().Trim() == mail.ToLower().Trim());
                if (contextUser == null)
                {
                    return(false);
                }

                var salt = contextUser.Salt;
                var hash = CalculatePasswordHash(salt, password);

                if (hash == contextUser.Hash)
                {
                    user = contextUser;
                    return(true);
                }
                return(false);
            }
        }
예제 #7
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Attempts to sign up from the given data. </summary>
        /// <remarks>   Andre Beging, 26.04.2018. </remarks>
        /// <param name="model">    The model. </param>
        /// <returns>   True if it succeeds, false if it fails. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static bool TrySignUp(SignUpModel model, out User user)
        {
            user = new User();

            if (string.IsNullOrWhiteSpace(model.Password))
            {
                return(false);
            }
            if (string.IsNullOrWhiteSpace(model.PasswordConfirmation))
            {
                return(false);
            }
            if (model.Password != model.PasswordConfirmation)
            {
                return(false);
            }
            if (string.IsNullOrWhiteSpace(model.Mail))
            {
                return(false);
            }

            using (var context = ContextHelper.OpenContext())
            {
                if (context.User.Any(u => u.Mail.Trim().ToLower() == model.Mail.Trim().ToLower()))
                {
                    return(false);
                }

                // Data valid

                var salt = GenerateSalt();
                var hash = CalculatePasswordHash(salt, model.Password);

                var newUser = new User
                {
                    Mail     = model.Mail.Trim(),
                    Username = model.Name,
                    Origin   = model.Origin.Trim(),
                    Created  = DateTime.Now,
                    Modified = DateTime.Now,
                    Salt     = salt,
                    Hash     = hash,
                    Type     = UserType.User,
                    Enabled  = true
                };

                context.User.Add(newUser);
                var result = context.SaveChanges();

                // No rows affected?
                if (result == 0)
                {
                    return(false);
                }

                user = newUser;

                // All good
                return(true);
            }
        }