Esempio n. 1
0
        public CommandResponse Execute()
        {
            var response = new CommandResponse()
            {
                Success = false
            };

            try
            {
                var item = db.Users.FirstOrDefault(w => w.ID == command.User.ID);

                if (item == null)
                {
                    item              = command.User;
                    item.ID           = Guid.NewGuid();
                    item.RegDate      = DateTime.Now;
                    item.PasswordHash = TAuth.Hash(item.PasswordHash);
                    item.State        = db.States.Where(s => s.Name == "Active").FirstOrDefault();
                    item.StateID      = db.States.Where(s => s.Name == "Active").FirstOrDefault().ID;

                    if (item.Role == null)
                    {
                        item.Role   = db.Roles.Where(r => r.Name == "User").FirstOrDefault();
                        item.RoleID = db.Roles.Where(r => r.Name == "User").FirstOrDefault().ID;
                    }
                    db.Users.Add(item);
                }
                else
                {
                    db.Entry(item);
                    item.Name    = command.User.Name;
                    item.Email   = command.User.Email;
                    item.RoleID  = command.User.RoleID;
                    item.StateID = command.User.StateID;
                    item.RegDate = command.User.RegDate;
                }

                db.SaveChanges();

                response.ID              = item.ID;
                response.Success         = true;
                response.ResponseObjects = new List <object>
                {
                    item.Name,
                    item.Role
                };
                response.Message = "Saved state.";
            }
            catch
            {
            }

            return(response);
        }
Esempio n. 2
0
        public JsonResult LogIn(string userStr)
        {
            bool   success = false;
            string message = "There was an error";

            try
            {
                User user   = JsonConvert.DeserializeObject <User>(userStr);
                User dbUser = repo.GetUserByEmail(user.Email);
                if (dbUser == null)
                {
                    message = "User with e-mail/password was not found";
                }
                else
                {
                    if (TAuth.Hash(user.PasswordHash) == dbUser.PasswordHash)
                    {
                        Session["User"]     = dbUser;
                        Session["UserID"]   = dbUser.ID.ToString();
                        Session["UserName"] = dbUser.Name;
                        Session["Role"]     = dbUser.Role.Name;

                        success = true;
                        message = "OK";
                    }
                    else
                    {
                        message = "User with e-mail/password was not found";
                    }
                }
            }
            catch
            {
            }

            return(Json(new { Success = success, Message = message }, JsonRequestBehavior.AllowGet));
        }