Esempio n. 1
0
        public HttpResponseMessage Post(GroupManagmentViewModel obj)
        {
            try
            {
                obj.ErrorMessage = String.Empty;

                obj.LoggedUser = Person.GetLoggedPerson(User);
                if (obj.LoggedUser.Status == StatusEnum.Deleted)
                {
                    obj.ErrorMessage = "Uprawnienia uzytkownika wygasly!";
                    return(Request.CreateResponse(HttpStatusCode.Created, obj));
                }

                if (obj.CurrentOrganization == null)
                {
                    obj.CurrentOrganization = db.Organizations.FirstOrDefault(x => x.OrganizationID == obj.LoggedUser.OrganizationID);
                }

                if (obj.CurrentOrganization == null)
                {
                    obj.ErrorMessage = "Brak organizacji do ktorej mozna przypisac grupe!";
                    return(Request.CreateResponse(HttpStatusCode.Created, obj));
                }

                switch (obj.ActionType)
                {
                case BaseActionType.Get:
                    //get groups assigned to organizaction
                    obj.Current = new ProfileGroup();


                    var groups = (from grp in db.GroupsInOrganizations
                                  join g in db.Groups on grp.ProfileGroupID equals g.ProfileGroupID
                                  where grp.OrganizationID == obj.CurrentOrganization.OrganizationID && g.Name != "Wszyscy" && !g.IsDeleted
                                  orderby g.CreateDate descending
                                  select new
                    {
                        ProfileGroupID = g.ProfileGroupID,
                        Name = g.Name
                    }
                                  ).ToList();

                    obj.Groups = new List <ProfileGroup>();

                    if (groups.Any())
                    {
                        obj.Groups = (from grp in groups
                                      group grp by grp.ProfileGroupID
                                      into gp
                                      select new ProfileGroup
                        {
                            ProfileGroupID = gp.Key,
                            Name = groups.FirstOrDefault(x => x.ProfileGroupID == gp.Key).Name
                        }).ToList();
                    }


                    foreach (var g in obj.Groups)
                    {
                        g.AssignedPeople = (from pig in db.PeopleInGroups
                                            join p in db.Users on pig.PersonID equals p.Id
                                            where pig.ProfileGroupID == g.ProfileGroupID
                                            select p).ToList();
                    }

                    obj.Success = String.Empty;

                    break;

                case BaseActionType.Delete:

                    obj.Current.IsDeleted     = true;
                    obj.Current.DeletedUserID = obj.LoggedUser.Id;
                    obj.Current.DeletedDate   = DateTime.Now;

                    var personsInGroups = (from t in db.PeopleInGroups
                                           where t.ProfileGroupID == obj.Current.ProfileGroupID
                                           select t).ToList();
                    if (personsInGroups != null && personsInGroups.Any())
                    {
                        db.PeopleInGroups.RemoveRange(personsInGroups);
                    }

                    var current = obj.Groups.FirstOrDefault(x => x.ProfileGroupID == obj.Current.ProfileGroupID);
                    if (current != null)
                    {
                        obj.Groups.Remove(current);
                    }

                    db.SaveChanges();

                    obj.Success = "Dane usuniete!";

                    break;

                case BaseActionType.Edit:

                    var group = db.Groups.FirstOrDefault(x => x.ProfileGroupID == obj.Current.ProfileGroupID);

                    group.Name = obj.Current.Name;

                    var toRemove = (from t in db.PeopleInGroups
                                    where t.ProfileGroupID == obj.Current.ProfileGroupID
                                    select t).ToList();
                    if (toRemove != null && toRemove.Any())
                    {
                        db.PeopleInGroups.RemoveRange(toRemove);
                        db.SaveChanges();
                    }

                    if (obj.Current.AssignedPeople != null && obj.Current.AssignedPeople.Any())
                    {
                        foreach (var item in obj.Current.AssignedPeople)
                        {
                            var pg = new ProfileGroup2Person();
                            pg.IsDeleted      = false;
                            pg.PersonID       = item.Id;
                            pg.ProfileGroupID = obj.Current.ProfileGroupID;
                            db.PeopleInGroups.Add(pg);
                        }
                        db.SaveChanges();
                    }

                    db.Entry(group).State = EntityState.Modified;

                    db.SaveChanges();

                    obj.Success = "Dane zapisane!";

                    break;

                case BaseActionType.Add:


                    obj.Current.CreateDate   = DateTime.Now;
                    obj.Current.CreateUserID = obj.LoggedUser.Id;
                    obj.Current.IsDeleted    = false;

                    db.Groups.Add(obj.Current);

                    var gip = new ProfileGroup2Organization();
                    gip.OrganizationID = obj.CurrentOrganization.OrganizationID;
                    gip.ProfileGroupID = obj.Current.ProfileGroupID;

                    db.GroupsInOrganizations.Add(gip);

                    db.SaveChanges();

                    //assigned people to group
                    if (obj.Current.AssignedPeople != null)
                    {
                        foreach (var item in obj.Current.AssignedPeople)
                        {
                            var pg = new ProfileGroup2Person();
                            pg.IsDeleted      = false;
                            pg.PersonID       = item.Id;
                            pg.ProfileGroupID = obj.Current.ProfileGroupID;
                            db.PeopleInGroups.Add(pg);
                        }
                        db.SaveChanges();
                    }

                    obj.Current = new ProfileGroup();

                    obj.ActionType = BaseActionType.Get;

                    Post(obj);

                    obj.Success = "Dane zapisane!";

                    break;

                default:
                    break;
                }

                return(Request.CreateResponse(HttpStatusCode.Created, obj));;
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Esempio n. 2
0
        public async Task <JsonResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                IdentityResult result         = new IdentityResult();
                Result         responseResult = new Result()
                {
                    Errors = new List <string>(), Succeeded = false
                };

                try
                {
                    var  user            = UserManager.FindById(model.UserId);
                    var  userByName      = UserManager.FindByName(model.UserName);
                    bool tokenValidation = await UserManager.UserTokenProvider.ValidateAsync("ResetPassword", model.Token, UserManager, user);

                    if (user == null || (DateTime.Now - user.InvitationDate).Days > 2 || !tokenValidation)
                    {
                        responseResult.Errors.Add("Nieprawidłowy token, lub token wygasł.");
                        return(Json(responseResult));
                    }

                    if (userByName != null && user.Id != userByName.Id)
                    {
                        responseResult.Errors.Add("Ten login jest już zajęty. Proszę wybrać inny.");
                        return(Json(responseResult));
                    }

                    user.RegistrationDate  = DateTime.Now;
                    user.ResetPasswordDate = DateTime.Now;
                    user.Status            = StatusEnum.Active;
                    user.UserName          = model.UserName;
                    result = UserManager.Update(user);


                    var group = _db.Groups.FirstOrDefault(x => x.Name == "Wszyscy");

                    if (group != null)
                    {
                        var userInGroup = new ProfileGroup2Person();
                        userInGroup.PersonID       = user.Id;
                        userInGroup.ProfileGroupID = group.ProfileGroupID;
                        _db.PeopleInGroups.Add(userInGroup);
                        _db.SaveChanges();
                    }

                    if (!result.Succeeded)
                    {
                        return(this.Json(result));
                    }

                    var rslt = await Person.ChangePasswordAsync(UserManager, new ResetPasswordViewModel()
                    {
                        Code            = model.Token,
                        Password        = model.Password,
                        ConfirmPassword = model.ConfirmPassword,
                        UserName        = user.UserName
                    }, false);

                    if (!rslt.Succeeded)
                    {
                        return(this.Json(rslt));
                    }

                    LogService.InsertUserLogs(OperationLog.UserRegistration, _db, user.Id, user.Id, user.OrganizationID.HasValue ? user.OrganizationID.Value : 0);

                    await UserManager.UpdateSecurityStampAsync(user.Id);

                    await UserManager.SendEmailAsync(user.Id,
                                                     "Rejestracja Kenpro",
                                                     "Zakończyłeś rejestrację. <br/>Twój login to: " + user.UserName
                                                     + "<br/>Twoja nazwa wyświetlana: " + user.DisplayName
                                                     + "<br/><a href=\"" + Request.Url.Scheme + "://" + Request.Url.Authority + "/signin\">Zaloguj się</a>");
                }
                catch (Exception ex)
                {
                }

                return(this.Json(result));
            }

            return(getErrorsFromModel());
        }