Exemplo n.º 1
0
        public async Task <ActionResult> Register(RegisterViewModel model, IEnumerable <string> Groups)

        {
            bool x = true;

            ViewBag.Current = "Users";

            ViewBag.DepartmentID = new SelectList(DepartmentListDisplay.CreateDepartmentListDisplay(), "Id", "Name");
            ViewBag.JobTitleId   = new SelectList(db.JobTitles.ToList(), "Id", "Name");

            ViewBag.Role = new SelectList(db.Roles.Where(a => !a.Name.Equals("Master")).ToList(), "Name", "Name", model.Role);

            ViewBag.Groups = new SelectList(db.Groups.ToList(), "Id", "Name");


            if (ModelState.IsValid)
            {
                if (db.Users.Any(a => a.UserName.Equals(model.UserName, StringComparison.OrdinalIgnoreCase)))
                {
                    ModelState.AddModelError("UserName", "اسم المستخدم موجود مسبقاً يرجى اعادة الإدخال");
                    x = false;
                }

                if (CheckJobTitleDepartment.CheckJobTitleDepartmentCreateUser(model.DepartmentID, model.JobTitleId) == false)
                {
                    ModelState.AddModelError("JobTitleId", "عددالأعضاء للقسم بالنسبة للمسمى الوظيفي وصل للحد الأعظمي");
                    x = false;
                }

                if (!string.IsNullOrEmpty(model.Email))
                {
                    if (db.Users.Any(a => a.Email.Equals(model.Email, StringComparison.OrdinalIgnoreCase)))
                    {
                        ModelState.AddModelError("Email", "لا يمكن أن يكون البريد الإلكتروني مكرر، يرجى إعادةالإدخال");

                        x = false;
                    }
                }


                if (x == false)
                {
                    return(View(model));
                }
                var user = new ApplicationUser
                {
                    UserName     = model.UserName,
                    Email        = model.Email,
                    FullName     = model.FullName,
                    Gender       = model.Gender,
                    DepartmentId = model.DepartmentID,
                    JobTitleId   = model.JobTitleId,
                    CreatedAt    = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss"),
                    CreatedById  = this.User.Identity.GetUserId(),
                    RoleName     = model.Role
                };

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

                if (result.Succeeded)
                {
                    await UserManager.AddToRoleAsync(user.Id, model.Role);


                    //Add User To Groups
                    if (Groups != null)
                    {
                        string       UserId           = User.Identity.GetUserId();
                        Notification notification     = null;
                        string       NotificationTime = string.Empty;
                        string       GroupName        = string.Empty;
                        foreach (string User_Group_Id in Groups)
                        {
                            var UserGroup = new UserGroup()
                            {
                                UserId      = user.Id,
                                GroupId     = Convert.ToInt32(User_Group_Id),
                                CreatedAt   = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss"),
                                CreatedById = this.User.Identity.GetUserId()
                            };

                            NotificationTime = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss");
                            db.UsersGroups.Add(UserGroup);
                            GroupName    = db.Groups.Find(UserGroup.GroupId).Name;
                            notification = new Notification()
                            {
                                CreatedAt = NotificationTime,
                                Active    = false,
                                UserId    = user.Id,
                                Message   = "تم إضافتك   إلى المجموعة  :" + GroupName
                                ,
                                NotificationOwnerId = UserId
                            };
                            db.Notifications.Add(notification);
                        }
                    }


                    db.SaveChanges();
                    return(RedirectToAction("Index", new { @Id = "CreateSuccess" }));
                }
                // AddErrors(result);
            }



            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult Edit(EditProfileViewModel EProfile, IEnumerable <string> Groups)

        {
            ViewBag.Current = "Users";
            string OldUserRole = db.Users.Find(EProfile.Id).RoleName;

            ViewBag.Role = new SelectList(db.Roles.ToList(), "Id", "Name", EProfile.Role);

            ViewBag.DepartmentID = new SelectList(DepartmentListDisplay.CreateDepartmentListDisplay(), "Id", "Name", EProfile.DepartmentID);
            ViewBag.JobTitleId   = new SelectList(db.JobTitles.ToList(), "Id", "Name", EProfile.JobTitleId);

            List <int> SelectedGroups = new List <int>();

            SelectedGroups = db.UsersGroups.Where(a => a.UserId.Equals(EProfile.Id)).Select(a => a.GroupId).ToList();
            SelectListItem        sl;
            List <SelectListItem> ListSl = new List <SelectListItem>();

            foreach (var G in db.Groups.ToList())
            {
                sl = new SelectListItem()
                {
                    Text     = G.Name,
                    Value    = G.Id.ToString(),
                    Selected = SelectedGroups.DefaultIfEmpty().Contains(G.Id) ? true : false
                };

                ListSl.Add(sl);
            }
            ViewBag.Groups = ListSl;
            bool            x    = true;
            ApplicationUser user = null;

            if (ModelState.IsValid)
            {
                user = UserManager.FindById(EProfile.Id);
                if (user == null)
                {
                    return(RedirectToAction("HttpNotFoundError", "ErrorController"));
                }

                //Delete All Users Permissions:
                if (!user.RoleName.Equals(EProfile.Role))
                {
                    List <PermissionsUser> User_Permissions = db.PermissionUsers.Where(a => a.UserId.Equals(user.Id)).ToList();
                    foreach (PermissionsUser UP in User_Permissions)
                    {
                        db.PermissionUsers.Remove(UP);
                    }
                    db.SaveChanges();
                }



                if (!EProfile.Role.Equals("Master"))
                {
                    if (CheckJobTitleDepartment.CheckJobTitleDepartmentCreateUser(EProfile.DepartmentID, EProfile.JobTitleId, EProfile.Id) == false)
                    {
                        ModelState.AddModelError("JobTitleId", "عددالأعضاء للقسم بالنسبة للمسمى الوظيفي وصل للحد الأعظمي");
                        x = false;
                    }
                }



                if (db.Users.Where(a => !a.Id.Equals(EProfile.Id)).Any(a => a.UserName.Equals(EProfile.UserName, StringComparison.OrdinalIgnoreCase)))
                {
                    ModelState.AddModelError("UserName", "لا يمكن أن يكون اسم امستخدم  مكرر، يرجى إعادةالإدخال");

                    x = false;
                }

                if (!string.IsNullOrEmpty(EProfile.Email))
                {
                    if (db.Users.Where(a => !a.Id.Equals(EProfile.Id)).Any(a => a.Email.Equals(EProfile.Email, StringComparison.OrdinalIgnoreCase)))
                    {
                        ModelState.AddModelError("Email", "لا يمكن أن يكون البريد الإلكتروني مكرر، يرجى إعادةالإدخال");

                        x = false;
                    }
                }

                if (x == false)
                {
                    return(View(EProfile));
                }
                user.FullName = EProfile.FullName;
                user.Email    = EProfile.Email;
                user.Gender   = EProfile.Gender;
                user.RoleName = EProfile.Role;
                user.UserName = EProfile.UserName;
                var HashPassword = UserManager.PasswordHasher.HashPassword(EProfile.Password);
                user.PasswordHash = HashPassword;

                user.UpdatedAt   = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss");
                user.UpdatedByID = this.User.Identity.GetUserId();

                db.Entry(user).State = System.Data.Entity.EntityState.Modified;
                //Add User To Groups

                //

                string       UserId           = User.Identity.GetUserId();
                Notification notification     = null;
                string       NotificationTime = string.Empty;
                string       GroupName        = string.Empty;
                NotificationTime = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss");

                notification = new Notification()
                {
                    CreatedAt           = NotificationTime,
                    Active              = false,
                    UserId              = user.Id,
                    Message             = "تم تحديث معلوماتك الشخصية",
                    NotificationOwnerId = UserId
                };
                db.Notifications.Add(notification);



                List <string> SelectedUserGroups = new List <string>();
                SelectedUserGroups = db.UsersGroups.Where(a => a.UserId.Equals(EProfile.Id)).Select(a => a.GroupId.ToString()).ToList();
                if (Groups != null)
                {
                    UserGroup     UserGroup    = null;
                    List <string> ExpectGroups = new List <string>();
                    ExpectGroups = SelectedUserGroups.Except(Groups).ToList();
                    foreach (string User_Group_Id in Groups)
                    {
                        NotificationTime = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss");

                        if (SelectedUserGroups.Contains(User_Group_Id))
                        {
                            continue;
                        }
                        UserGroup = new UserGroup()
                        {
                            UserId      = user.Id,
                            GroupId     = Convert.ToInt32(User_Group_Id),
                            CreatedAt   = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss"),
                            CreatedById = this.User.Identity.GetUserId()
                        };

                        db.UsersGroups.Add(UserGroup);
                        GroupName    = db.Groups.Find(UserGroup.GroupId).Name;
                        notification = new Notification()
                        {
                            CreatedAt = NotificationTime,
                            Active    = false,
                            UserId    = user.Id,
                            Message   = "تم إضافتك   إلى المجموعة  :" + GroupName
                            ,
                            NotificationOwnerId = UserId
                        };
                        db.Notifications.Add(notification);
                    }


                    UserGroup deleteUserGroup;
                    foreach (string s in ExpectGroups)
                    {
                        deleteUserGroup = db.UsersGroups.Where(a => a.UserId.Equals(EProfile.Id) && a.GroupId.ToString().Equals(s)).SingleOrDefault();

                        db.UsersGroups.Remove(deleteUserGroup);
                        GroupName    = db.Groups.Find(deleteUserGroup.GroupId).Name;
                        notification = new Notification()
                        {
                            CreatedAt = NotificationTime,
                            Active    = false,
                            UserId    = user.Id,
                            Message   = "تم إزالتك   من المجموعة  :" + GroupName
                            ,
                            NotificationOwnerId = UserId
                        };
                        db.Notifications.Add(notification);
                    }
                    db.SaveChanges();
                }

                else
                {
                    foreach (UserGroup ug in db.UsersGroups.Where(a => a.UserId.Equals(EProfile.Id)))
                    {
                        db.UsersGroups.Remove(ug);
                        GroupName    = db.Groups.Find(ug.GroupId).Name;
                        notification = new Notification()
                        {
                            CreatedAt = NotificationTime,
                            Active    = false,
                            UserId    = user.Id,
                            Message   = "تم إزالتك   من المجموعة  :" + GroupName
                            ,
                            NotificationOwnerId = UserId
                        };
                        db.Notifications.Add(notification);
                    }

                    db.SaveChanges();
                }

                db.SaveChanges();



                return(RedirectToAction("Index", new { @Id = "EditSuccess" }));
            }



            return(View(EProfile));
        }