public static void RemoveUser(FrontEditorContext _context, int uId)
        {
            User           user     = _context.Users.Where(x => x.Id == uId).FirstOrDefault();
            List <Project> projects = _context.Projects.Where(x => x.Owner == user).ToList();

            foreach (Project proj in projects)
            {
                _context.Projects.Remove(proj);
            }
            _context.Users.Remove(user);
            _context.SaveChanges();
        }
        public static UserViewModel SaveUserProfile(FrontEditorContext _context, UserViewModel model)
        {
            User user = _context.Users.Where(x => x.Id == model.UserId).FirstOrDefault();

            user.DisplayName        = model.DisplayName;
            user.UserName           = model.UserName;
            user.NormalizedUserName = model.UserName.ToUpper();
            user.Email           = model.Email;
            user.NormalizedEmail = model.Email.ToUpper();
            _context.SaveChanges();
            return(new UserViewModel(user, model.RoleId));
        }
        public static void SetProjectStatus(FrontEditorContext _context, int projectId, ProjectStatus status, bool addExport = false)
        {
            Project project = _context.Projects.Where(x => x.Id == projectId).FirstOrDefault();

            if (project != null)
            {
                project.Status = status;
                if (addExport)
                {
                    project.ExportCount++;
                }
                _context.Update(project);
                _context.SaveChanges();
            }
        }
        public static EditorModelData GetProjectEditorData(FrontEditorContext _context, int projectId)
        {
            ProjectJSON data = _context.ProjectJSONDatas.Where(x => x.ProjectId == projectId).FirstOrDefault();

            if (data == null)
            {
                data = new ProjectJSON()
                {
                    ProjectId   = projectId,
                    ProjectData = parser.SerializeData(new EditorModelData())
                };
                _context.Add(data);
                _context.SaveChanges();
            }
            return(parser.DeserializeData(data.ProjectData));
        }
        public static void DeleteProject(FrontEditorContext _context, int projectId, int userId)
        {
            Project project = _context.Projects.Where(x => x.Id == projectId).FirstOrDefault();

            if (!UsersBL.UserIsAdmin(_context, userId) && project.Owner.Id != userId)
            {
                throw new Exception("Nincs jogosultsága a kért művelet elvégzéséhez!");
            }
            ProjectJSON jsondata = _context.ProjectJSONDatas.Where(x => x.ProjectId == projectId).FirstOrDefault();

            if (jsondata != null)
            {
                _context.ProjectJSONDatas.Remove(jsondata);
            }
            _context.Projects.Remove(project);
            _context.SaveChanges();
        }
        public static UserViewModel EditUserProfile(FrontEditorContext _context, UserViewModel model)
        {
            User user = _context.Users.Where(x => x.Id == model.UserId).FirstOrDefault();

            user.DisplayName        = model.DisplayName;
            user.UserName           = model.UserName;
            user.NormalizedUserName = model.UserName.ToUpper();
            user.Email           = model.Email;
            user.NormalizedEmail = model.Email.ToUpper();
            IdentityUserRole <int> userRole = _context.UserRoles.Where(x => x.UserId == model.UserId).FirstOrDefault();

            userRole.RoleId = model.RoleId;
            _context.SaveChanges();
            return(new UserViewModel(user, model.RoleId)
            {
                DatasChanged = true
            });
        }
 public static void ImportProjectFromZip(FrontEditorContext _context, int projectId, string rootPath, string zipPath)
 {
     try
     {
         string tempPath   = rootPath + "/import/" + projectId + "-import-" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + "/";
         string imagesPath = rootPath + "/images/projects/" + projectId + "/";
         ZipFile.ExtractToDirectory(zipPath, tempPath);
         //Parse json and Save it
         string          importedData = System.IO.File.ReadAllText(tempPath + "project.json");
         EditorModelData data         = parser.DeserializeData(importedData);
         ProjectJSON     jsonData     = _context.ProjectJSONDatas.Where(x => x.ProjectId == projectId).FirstOrDefault();
         if (jsonData == null)
         {
             jsonData = new ProjectJSON()
             {
                 ProjectId   = projectId,
                 ProjectData = parser.SerializeData(data)
             };
             _context.Add(jsonData);
         }
         else
         {
             jsonData.ProjectData = parser.SerializeData(data);
             _context.Update(jsonData);
         }
         _context.SaveChanges();
         //Empty files folder and copy new files to files folder
         foreach (string file in Directory.GetFiles(imagesPath, "*.*", SearchOption.AllDirectories))
         {
             File.Delete(file);
         }
         foreach (string file in Directory.GetFiles(tempPath + "images/", "*.*", SearchOption.AllDirectories))
         {
             File.Copy(file, file.Replace(imagesPath, tempPath), true);
         }
         //Delete import files from temp folder
         Directory.Delete(tempPath, true);
     }
     catch (Exception ex)
     {
         throw new Exception("Hiba történta projekt impotálása közben!");
     }
 }
        public static void SaveProjectEditorData(FrontEditorContext _context, int projectId, EditorModelData saveData)
        {
            ProjectJSON data = _context.ProjectJSONDatas.Where(x => x.ProjectId == projectId).FirstOrDefault();

            if (data == null)
            {
                data = new ProjectJSON()
                {
                    ProjectId   = projectId,
                    ProjectData = parser.SerializeData(saveData)
                };
                _context.Add(data);
            }
            else
            {
                data.ProjectData = parser.SerializeData(saveData);
                _context.Update(data);
            }
            _context.SaveChanges();
        }
        public static ProjectViewModel EditProjectData(FrontEditorContext _context, ProjectViewModel proj)
        {
            Project project = _context.Projects.Where(x => x.Id == proj.ProjectId).FirstOrDefault();

            project.Title       = proj.Title;
            project.Description = proj.Description;
            project.Category    = proj.Category;
            project.LastEdit    = DateTime.Now;
            if (proj.OwnerId < 0)
            {
                User owner = _context.Users.Where(x => x.Id == proj.OwnerId).FirstOrDefault();
                project.Owner = owner;
            }
            project.Status = proj.Status;

            _context.Projects.Update(project);
            _context.SaveChanges();
            return(new ProjectViewModel(project)
            {
                DatasChanged = true
            });
        }
        public static ProjectViewModel CreateProject(FrontEditorContext _context, ProjectViewModel proj)
        {
            User    owner   = _context.Users.Where(x => x.Id == proj.OwnerId).FirstOrDefault();
            Project project = new Project()
            {
                Title       = proj.Title,
                Description = proj.Description,
                Category    = proj.Category,
                CreateTime  = DateTime.Now,
                Owner       = owner,
                LastEdit    = DateTime.Now,
                Status      = ProjectStatus.New,
                ExportCount = 0
            };

            _context.Projects.Add(project);
            _context.SaveChanges();
            return(new ProjectViewModel(project)
            {
                DatasChanged = true, NewProject = true
            });
        }
        public static async Task <LoginViewModel> Login(FrontEditorContext _context, SignInManager <User> _signInManager, LoginViewModel model)
        {
            User usr = _context.Users.Where(x => x.Email == model.Email).FirstOrDefault();

            if (usr == null)
            {
                model.ErrorText += "Felhasználó nem található!";
            }
            else
            {
                var result = await _signInManager.PasswordSignInAsync(usr, model.Password, model.RememberMe, false);

                if (result.Succeeded)
                {
                    usr.LastActive = DateTime.Now;
                    _context.SaveChanges();
                }
                else
                {
                    model.ErrorText += "Nem megfelelő bejelentkezési adatok!";
                }
            }
            return(model);
        }
        public static async Task <UserViewModel> CreateUser(FrontEditorContext _context, UserManager <User> _userManager, UserViewModel model)
        {
            try
            {
                User user = _context.Users.Where(x => x.Email == model.Email).FirstOrDefault();
                if (user != null)
                {
                    throw new Exception("Ezzel az e-mail címmel már létezik felhasználó!");
                }
                else
                {
                    user = new User
                    {
                        DisplayName  = model.DisplayName,
                        UserName     = model.UserName,
                        Email        = model.Email,
                        LastActive   = DateTime.Now,
                        Registration = DateTime.Now
                    };
                    model.NewPassword = GeneratePassword(_userManager);
                    var result = await _userManager.CreateAsync(user, model.NewPassword);

                    if (result.Succeeded)
                    {
                        _context.UserRoles.Add(new IdentityUserRole <int>()
                        {
                            UserId = user.Id, RoleId = model.RoleId
                        });
                        _context.SaveChanges();
                        model.UserId = user.Id;
                    }
                    foreach (var error in result.Errors)
                    {
                        if (error.Code == "DuplicateUserName")
                        {
                            model.ErrorText += "Ezzel a felhasználónévvel már létezik felhasználó!<br/>";
                        }
                        else if (error.Code == "PasswordTooShort")
                        {
                            model.ErrorText += "A jelszó legalább 6 karakter!<br/>";
                        }
                        else if (error.Code == "PasswordRequiresNonAlphanumeric")
                        {
                            model.ErrorText += "A jelszó tartalmaz legalább egy nem alfanumerikus karaktert!<br/>";
                        }
                        else if (error.Code == "PasswordRequiresDigit")
                        {
                            model.ErrorText += "A jelszó legalább egy számot tartalmaz!<br/>";
                        }
                        else if (error.Code == "PasswordRequiresUpper")
                        {
                            model.ErrorText += "A jelszó legalább egy nagybetűt tartalmaz!<br/>";
                        }
                        else if (error.Code == "PasswordRequiresLower")
                        {
                            model.ErrorText += "A jelszó legalább egy kisbetűt tartalmaz!<br/>";
                        }
                        else
                        {
                            model.ErrorText += error.Description + "<br/>";
                        }
                    }
                    model.NewUser      = true;
                    model.Registration = DateTime.Now;
                    model.LastActive   = model.Registration;
                    model.DatasChanged = true;
                }
            }
            catch (Exception ex)
            {
                model.ErrorText = ex.Message;
            }
            return(model);
        }
        public static async Task <RegisterViewModel> Register(FrontEditorContext _context, UserManager <User> _userManager, SignInManager <User> _signInManager, RegisterViewModel model)
        {
            User user = _context.Users.Where(x => x.Email == model.Email).FirstOrDefault();

            if (user != null)
            {
                model.ErrorText += "Ezzel az e-mail címmel már létezik felhasználó!";
            }
            else
            {
                user = new User
                {
                    DisplayName  = model.DisplayName,
                    UserName     = model.UserName,
                    Email        = model.Email,
                    LastActive   = DateTime.Now,
                    Registration = DateTime.Now
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _context.UserRoles.Add(new IdentityUserRole <int>()
                    {
                        UserId = user.Id, RoleId = 2
                    });
                    _context.SaveChanges();
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        if (error.Code == "DuplicateUserName")
                        {
                            model.ErrorText += "Ezzel a felhasználónévvel már létezik felhasználó!<br />";
                        }
                        else if (error.Code == "PasswordTooShort")
                        {
                            model.ErrorText += "A jelszó legalább 6 karakter!<br />";
                        }
                        else if (error.Code == "PasswordRequiresNonAlphanumeric")
                        {
                            model.ErrorText += "A jelszó tartalmaz legalább egy nem alfanumerikus karaktert!<br />";
                        }
                        else if (error.Code == "PasswordRequiresDigit")
                        {
                            model.ErrorText += "A jelszó legalább egy számot tartalmaz!<br />";
                        }
                        else if (error.Code == "PasswordRequiresUpper")
                        {
                            model.ErrorText += "A jelszó legalább egy nagybetűt tartalmaz!<br />";
                        }
                        else if (error.Code == "PasswordRequiresLower")
                        {
                            model.ErrorText += "A jelszó legalább egy kisbetűt tartalmaz!<br />";
                        }
                        else
                        {
                            model.ErrorText += error.Description + "<br />";
                        }
                    }
                }
            }
            return(model);
        }