示例#1
0
        public ApiResponse GetDetail(int id)
        {
            this.CheckCurrentUserPermission(id, ((x) => x.SectionUsersVisible));

            using (var context = new AgmDataContext())
            {
                var currentUser = this.GetCurrentUser();

                var user = context.Users.FirstOrDefault(u => u.Id == id && !u._isDeleted);
                if (user == null && id == 0)
                {
                    user = new User();
                }

                if (!currentUser.SectionUsersVisible)
                {
                    user.IdExport = -1;
                }

                return(new ApiResponse(true)
                {
                    Data = user
                });
            }
        }
示例#2
0
        public ApiResponse Set(User user)
        {
            this.CheckCurrentUserPermission(user.Id, ((x) => x.SectionUsersVisible || x.IsAdmin));
            var currentUser = this.GetCurrentUser();

            using (var context = new AgmDataContext())
            {
                if (user._image.Contains("/Temp"))
                {
                    File.Move(HttpContext.Current.Server.MapPath(user._image), HttpContext.Current.Server.MapPath(user._image.Replace("/Temp", string.Empty)));
                    user.Image = user._image.Replace("/Temp", string.Empty);
                }

                if (user.IdExport.HasValue && context.Users.Any(u => u.IdExport == user.IdExport && u.Id != user.Id && !u._isDeleted))
                {
                    var suggestedId = (context.Users.Any(u => u.IdExport != null && !u._isDeleted)) ? context.Users.Where(u => u.IdExport != null && !u._isDeleted).Max(u => u.IdExport).Value + 1 : 1;
                    return(new ApiResponse(false)
                    {
                        Errors = new List <ApiResponseError>()
                        {
                            new ApiResponseError()
                            {
                                Code = -2, Message = string.Format("ID Export già utilizzato. ID Export suggerito:{0}", suggestedId)
                            }
                        }.ToArray()
                    });
                }

                if (user.Id != 0 && context.Users.Any(u => u.Id == user.Id && !u._isDeleted))
                {
                    context.Users.Attach(user);
                    ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);

                    if (!currentUser.SectionUsersVisible && !currentUser.IsAdmin)
                    {
                        context.Entry(user).Property(x => x.IdExport).IsModified  = false;
                        context.Entry(user).Property(x => x._isActive).IsModified = false;
                        context.Entry(user).Property(x => x._sectionMonthlyReportsVisible).IsModified = false;
                    }

                    if (!currentUser.IsAdmin)
                    {
                        context.Entry(user).Property(x => x._sectionJobAdsVisible).IsModified        = false;
                        context.Entry(user).Property(x => x._sectionJobApplicantsVisible).IsModified = false;
                        context.Entry(user).Property(x => x._sectionUsersVisible).IsModified         = false;
                        context.Entry(user).Property(x => x._sectionExportVisible).IsModified        = false;
                        context.Entry(user).Property(x => x._canSendMessage).IsModified = false;
                        context.Entry(user).Property(x => x.RetributionItemConfSerialized).IsModified = false;
                        context.Entry(user).Property(x => x._isShiftWorker).IsModified = false;
                        context.Entry(user).Property(x => x._userType).IsModified      = false;
                    }
                }
                else
                {
                    if (context.Users.Any(u => u.Email.ToLower() == user.Email.ToLower() && !u._isDeleted))
                    {
                        return new ApiResponse(false)
                               {
                                   Errors = new List <ApiResponseError>()
                                   {
                                       new ApiResponseError()
                                       {
                                           Code = -1, Message = "Utente già esistente"
                                       }
                                   }.ToArray()
                               }
                    }
                    ;

                    if (user.Id != 0)
                    {
                        user.Id = 0;
                    }
                    var insNewUser = context.Users.Add(user);
                    insNewUser._image = null;
                }

                var res = context.SaveChanges();

                if (res > 0)
                {
                    return(new ApiResponse(true));
                }

                return(new ApiResponse(false));
            }
        }