Exemplo n.º 1
0
        public Result Login(EmployerEntity entity)
        {
            var result = new Result();

            try
            {
                var con         = new DapperConnectionManager();
                var query       = new QueryEntity();
                var credentials = new CredentialsManager();
                entity.Email = entity.Email.Trim().ToLower();
                query.Query  = @"SELECT * FROM Employers
                            where Email = @Email and Active = 1 and ApnaUser = 0";
                query.Entity = entity;
                result       = con.ExecuteQuery <EmployerEntity>(query);

                if (!result.Success)
                {
                    result.Message = "Login error";
                    return(result);
                }

                var r = (IEnumerable <EmployerEntity>)result.Entity;

                var employer = r.FirstOrDefault();

                if (employer == null)
                {
                    result.Message = "Invalid password or username";
                    result.Success = false;
                    result.Entity  = null;
                    return(result);
                }
                var password = credentials.EncodePassword(entity.Password, employer.Hash);
                if (password == employer.Password)
                {
                    employer.Hash     = null;
                    employer.Password = null;
                    employer.Token    = credentials.GenerateEmployerToken(employer);
                    result.Entity     = employer;
                    return(result);
                }
                result.Entity  = null;
                result.Message = "Employer not found";
                result.Success = false;
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                result.Entity  = null;
                result         = result ?? new Result(false);
                result.Message = "An error occurred";
            }

            return(result);
        }
Exemplo n.º 2
0
        public Result Register(EmployerEntity entity)
        {
            var result = new Result();

            try
            {
                if (entity.Password.Length < 6)
                {
                    result         = new Result(false);
                    result.Message = "Password length invalid";
                    return(result);
                }

                var con   = new DapperConnectionManager();
                var query = new QueryEntity();

                var credentials = new CredentialsManager();

                var hash = credentials.GenerateSalt();

                entity.Password = credentials.EncodePassword(entity.Password, hash);
                entity.Hash     = hash;
                if (!entity.Email.Contains("@") || entity.Email.Length < 3)
                {
                    result         = new Result(false);
                    result.Message = "Email invalid";
                    return(result);
                }
                entity.Email = entity.Email.Trim().ToLower();

                var queryCheckEmail = new QueryEntity()
                {
                    Entity = new { Email = entity.Email },
                    Query  = @"SELECT Email from Employers where Email = @Email and Active = 1"
                };
                var resultCheckEmail = con.ExecuteQuery <EmployerEntity>(queryCheckEmail);
                var dump             = ObjectDumper.Dump(resultCheckEmail);
                if (!resultCheckEmail.Success)
                {
                    resultCheckEmail.Entity  = null;
                    resultCheckEmail.Success = false;
                    resultCheckEmail.Message = "An error occurred with email check";
                    return(resultCheckEmail);
                }
                var checkEmail = (IEnumerable <EmployerEntity>)resultCheckEmail.Entity;
                if (checkEmail.Any())
                {
                    resultCheckEmail.Entity  = null;
                    resultCheckEmail.Success = false;
                    resultCheckEmail.Message = "The email is currently in use";
                    return(resultCheckEmail);
                }

                entity.CreateDate = DateTime.Now;
                entity.ModifyDate = DateTime.Now;

                query.Entity = entity;
                query.Query  = @"INSERT INTO Employers (Email, EmployerName, Password, Hash, CreateDate, ModifyDate) VALUES(@Email, @EmployerName, @Password, @Hash, @CreateDate, @ModifyDate)";

                result = con.InsertQuery(query);
                if (result.Success)
                {
                    entity.EmployerId = (int)result.Entity;
                    entity.Password   = "";
                    entity.Hash       = "";
                    entity.Token      = credentials.GenerateEmployerToken(entity);
                    result.Entity     = entity;

                    Task.Run(() => new EmailManager().SendEmail(entity.Email, DL.Models.EmailType.Welcome, new
                    {
                        CurrentUserName  = entity.EmployerName,
                        CurrentUserEmail = entity.Email
                    }));
                }
                result.Message = result.Success ? "The employer has been created" : "An error occurred";
            }
            catch (Exception ex)
            {
                if (result == null)
                {
                    result = new Result();
                }
                Logger.Log(ex);
                result.Entity  = null;
                result.Success = false;
                result.Message = "An error occurred with exception";
            }
            return(result);
        }