public ActionResult New(AccessViewModel model, string accessLevel)
        {
            if (!isAuthenticated() || !loggedUser.isAdmin)
                return new HttpUnauthorizedResult();
            else
            {
                if (ModelState.IsValid)
                {
                    if (!new User().emailExists(model.Email))
                    {
                        Common common = new Common();
                        string newPassword = common.generatePassword(6);
                        bool emailSent = common.sendEmail(model.Email, string.Format("Seu acesso foi criado no Agenda SUD\n\nEmail: {0}\nSenha: {1}\n\nAgenda SUD\nhttp://lds.toughland.com", model.Email, newPassword), "Agenda SUD - Acesso Criado");
                        Result result = new User().addUser(model.Email, newPassword, loggedUser.Unit, (accessLevel == "A"));

                        if (emailSent && result.Success)
                            return RedirectToAction("Index");
                        else
                        {
                            ModelState.AddModelError("", "Ocorreu um erro ao criar novo acesso");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Endereço de email já está sendo utilizado por outro usuário");
                    }
                }
                return View(model);
            }
        }
 public ActionResult ForgotPassword(string email)
 {
     User user = new User().getUser(email.ToLower());
     if (user.Id == 0)
         return RedirectToAction("Feedback", new { message = "Usuário não encontrado" });
     else
     {
         Common common = new Common();
         string newPassword = common.generatePassword(6);
         Result result = user.updateUser(user.Id, email.ToLower(), newPassword);
         if (result.Success && common.sendEmail(email, string.Format("Sua nova senha é\n\n{0}\n\nAgenda SUD\nhttp://lds.toughland.com", newPassword), "Agenda SUD - Nova senha"))
         {
             return RedirectToAction("Feedback", new { message = "Uma nova senha foi enviada para seu endereço de email" });
         }
         else
         {
             return RedirectToAction("Feedback", new { message = "Erro ao enviar nova senha" });
         }
     }
 }
        public ActionResult Update(UserViewModel model)
        {
            if (!isAuthenticated())
                return new HttpUnauthorizedResult();
            else
            {
                if (ModelState.IsValid)
                {
                    if (model.Password != string.Empty && model.Email != string.Empty)
                    {
                        Models.User user = new Models.User();
                        string oldEmail = user.getUser(model.Id).Email;
                        bool emailOk = false;

                        // check if the email id different
                        if (oldEmail.ToLower() == model.Email.ToLower())
                            emailOk = true;
                        else
                        {
                            if (!user.emailExists(model.Email))
                                emailOk = true;
                        }

                        if (emailOk)
                        {
                            Result result = user.updateUser(model.Id, model.Email, model.Password);

                            if (result.Success)
                                result = new Unit().updateUnit(model.UnitName, model.Id);

                            if (result.Success)
                            {
                                Session["user"] = new User().getUser(model.Id);
                                return RedirectToAction("UpdateSuccess");
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("", "Endereço de email já está sendo utilizado por outro usuário");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Os campos email e senha precisam ser preenchidos");
                    }
                }

                ViewBag.isAdmin = loggedUser.isAdmin;

                return View(model);
            }
        }
Beispiel #4
0
 public List<User> getWardUsers(int unitId)
 {
     List<User> users = new List<User>();
     foreach (List<string> data in database.retrieveData("select us.id, us.email, us.profile from lds_user us, lds_unit un, lds_user_unit uu " +
         " where unit_id = @unit_id " +
         "   and uu.unit_id = un.id " +
         "   and uu.user_id = us.id " +
         " order by us.email", unitId))
     {
         User user = new User();
         user.Id = common.convertNumber(data[0]);
         user.Email = data[1];
         user.isAdmin = (data[2] == "1");
         users.Add(user);
     }
     return users;
 }
Beispiel #5
0
 public User getUser(string email)
 {
     User user = new User();
     foreach (List<string> data in database.retrieveData("select id, profile from lds_user where email = @email", email))
     {
         user.Id = Convert.ToInt32(data[0]);
         user.Email = email;
         user.isAdmin = (data[1] == "1");
         Unit unit = new Unit().getUnit(user.Id);
         user.Unit = unit.Id;
         user.UnitName = unit.Name;
     }
     return user;
 }
Beispiel #6
0
 public User getUser(int id)
 {
     User user = new User();
     foreach(List<string> data in database.retrieveData("select email, password, profile from lds_user where id = @id", id))
     {
         user.Id = id;
         user.Email = data[0];
         user.Password = data[1];
         user.isAdmin = (data[2] == "1");
         Unit unit = new Unit().getUnit(id);
         user.Unit = unit.Id;
         user.UnitName = unit.Name;
     }
     return user;
 }