public async Task Find_Correct()
        {
            var service    = new UserRoleAppService(this.CreateDbContext);
            var userId     = CreateId();
            var roleId     = CreateId();
            var systemRole = new SystemRole()
            {
                ClientIds = "1,2,3",
                ID        = roleId,
                Name      = "Name"
            };
            var userRole = new UserRole()
            {
                RoleID = roleId,
                UserID = userId
            };

            using (var db = this.CreateDbContext())
            {
                db.SystemRoles.Add(systemRole);
                db.UserRoles.Add(userRole);
                await db.SaveChangesAsync();
            }

            var role = await service.FindRolesByUserIdAsync(userId);

            Assert.IsNotNull(role);
            Assert.IsTrue(role.Count == 1);
            using (var db = this.CreateDbContext())
            {
                db.SystemRoles.Remove(systemRole);
                db.UserRoles.Remove(userRole);
                await db.SaveChangesAsync();
            }
        }
Beispiel #2
0
        public async Task Delete_Correct()
        {
            var service = new UserRoleAppService(this.CreateDbContext);
            var roleId  = CreateId();
            var userId  = CreateId();

            using (var db = this.CreateDbContext())
            {
                db.UserRoles.Add(new UserRole()
                {
                    UserID = userId,
                    RoleID = roleId
                });
                await db.SaveChangesAsync();
            }

            await service.DeleteRoleUsersAsync(roleId, new List <string>()
            {
                userId
            });

            var result = await service.FindUsersByRoleIdAsync(roleId);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count == 0);
        }
        public async Task Find_Emtpty()
        {
            var service = new UserRoleAppService(this.CreateDbContext);
            var roles   = await service.FindRolesByUserIdAsync(CreateId());

            Assert.IsNotNull(roles);
            Assert.IsTrue(roles.Count == 0);
        }
        public async Task Create_Correct()
        {
            var service = new UserRoleAppService(this.CreateDbContext);
            var roleId  = CreateId();
            var userId  = CreateId();
            var result  = await service.CreateRoleUsersAsync(roleId, new List <string>()
            {
                userId
            });

            //因为用户Id 以及 角色Id不是已经存在的
            Assert.IsTrue(result);
        }
Beispiel #5
0
        public async Task Create_Correct()
        {
            var service = new UserRoleAppService(this.CreateDbContext);
            var roleId  = CreateId();
            var userId  = CreateId();
            var result  = await service.CreateUsersRoleAsync(userId, new List <string>()
            {
                roleId
            });

            Assert.IsTrue(result);
            using (var db = this.CreateDbContext())
            {
                var entitys = db.UserRoles.Where(u => u.UserID == userId).ToList();
                Assert.IsTrue(entitys.Count == 1);
                db.UserRoles.RemoveRange(entitys);
                await db.SaveChangesAsync();
            }
        }
        public DataTablesResult<EmployeeViewModel> GetAllEmployees(DataTablesParam dataTableParam)
        {

            var svc = new EmployeeAppService();
            var users = svc.GetAllEmployee();
            var usersVm = new List<EmployeeViewModel>();
            var userRoleSvc = new UserRoleAppService();
            


            foreach (var itm in users)
            {

                var itmVm = new EmployeeViewModel
                {
                    FullName = itm.FullName,
                    UserName = itm.User.UserName,
                    EmployeeTypeName=itm.EmployeeType.Name,
                    Mobile = itm.Mobile,
                    UserStateName = itm.User.UserState.Description,
                    EMail = "<a title=\"Escribir a " + itm.FullName + "...\" target=\"_blank\" href=\"mailto:" + itm.EMail + "\"><i class=\"fa fa-envelope\"></i>&nbsp;" + itm.EMail + "</a>",
                    EmployeeId = itm.EmployeeId,
                    EmployeeTypeId = itm.EmployeeTypeId,
                    UserId = itm.User.UserId,
                    Phone = itm.Phone,

                };
                var currentRoles = userRoleSvc.GetUserRoleByUserId(itm.UserId);
                foreach (var userRole in currentRoles)
                {
                    itmVm.RoleNames += userRole.Role.Name + ",";
                }

                itmVm.RoleNames = itmVm.RoleNames.Remove(itmVm.RoleNames.Length - 1);

                if (!System.IO.File.Exists(Server.MapPath("~/Employee_Files/" + itm.EmployeeId + "/")))
                {
                    Directory.CreateDirectory(Server.MapPath("~/Employee_Files/" + itm.EmployeeId + "/"));
                }
             

                

                if (itm.Photo != null)
                {
                    if (System.IO.File.Exists(Server.MapPath("~/Employee_Files/" + itm.EmployeeId + "/" + itm.Photo)))
                    {

                        byte[] image =
                            System.IO.File.ReadAllBytes(
                                Server.MapPath("~/Employee_Files/" + itm.EmployeeId + "/" + itm.Photo));
                        var base64 = Convert.ToBase64String(image);
                        var imgSrc = String.Format("data:image/png;base64,{0}", base64);
                        itmVm.PhotoEncodeSource = "<img src=\"" + imgSrc + "\" class=\"photo-table-image\"/>";
                    }
                    else
                    {
                        itmVm.PhotoEncodeSource = "Image Error";
                    }
                }

                var sb = new StringBuilder();

                string editUrl = Url.Action("Edit", "Employee");
                string specialtyUrl = Url.Action("Index", "MedicSpecialty");
                string officeUrl = Url.Action("Index", "Office");
                string scheduleUrl = Url.Action("Index", "Schedule");
                sb.AppendLine("<div class=\"btn-group\">");
                sb.AppendLine(
                    "<button type=\"button\" class=\"btn btn-default dropdown-toggle\" data-toggle=\"dropdown\" aria-expanded=\"false\">");
                sb.AppendLine("Acciones <span class=\"caret\"></span>");
                sb.AppendLine("</button>");
                sb.AppendLine("<ul class=\"dropdown-menu\" role=\"menu\">");
                sb.AppendLine("<li><a href=\"" + editUrl + "?id=-1\"><i class=\"fa fa-plus\"></i>&nbsp;Nuevo Empleado</a></li>");
                sb.AppendLine("<li><a href=\"" + editUrl + "?id=" + itmVm.EmployeeId + "\"><i class=\"fa fa-edit\"></i>&nbsp;Editar " + itmVm.FullName + "</a></li>");

                if (itmVm.EmployeeTypeId != 2) {
                    sb.AppendLine("<li><a href=\"" + specialtyUrl + "?id=" + itmVm.EmployeeId + "\"><i class=\"fa fa-edit\"></i>&nbsp;Especialidades " + itmVm.FullName + "</a></li>");
                sb.AppendLine("<li><a href=\"" + officeUrl + "?id=" + itmVm.EmployeeId + "\"><i class=\"fa fa-edit\"></i>&nbsp;Oficinas de " + itmVm.FullName + "</a></li>");
                sb.AppendLine("<li><a href=\"" + scheduleUrl + "?id=" + itmVm.EmployeeId + "\"><i class=\"fa fa-edit\"></i>&nbsp;Agenda de " + itmVm.FullName + "</a></li>");
                
                }

                sb.AppendLine("</ul>");
                sb.AppendLine("</div>");






                var actionButton = sb.ToString();

                itmVm.ActionButton = actionButton;
                usersVm.Add(itmVm);

            }

            var usersVmQueryable = usersVm.AsQueryable();


            return DataTablesResult.Create(usersVmQueryable, dataTableParam);

        }
        public ActionResult Edit(UserViewModel model)
        {
            model.UserStates = GetUserStates(); //Reload List For Clean Model

            try
            {
                var svc = new UserAppService();

                var o = new User
                {
                    UserName = model.UserName,
                    
                    UserStateId = model.UserStateId,
                    HasChangePassword = model.HasChangePassword


                };
                if (model.UserId == -1)
                {
                    string salt;
                    var hashedPassword = WebHelper.EncryptPassword(model.Password, out salt);
                    o.Password = hashedPassword;
                    o.Salt = salt;
                }
                else
                {
                    if (String.IsNullOrEmpty(model.Password))
                    {
                        var ousr = new UserAppService().GetUser(model.UserId);

                        o.Password = ousr.Password;
                        o.Salt = ousr.Salt;

                    }
                    else
                    { //GetCurrentData
                        string salt;
                        var hashedPassword = WebHelper.EncryptPassword(model.Password, out salt);
                        o.Password = hashedPassword;
                        o.Salt = salt;
                        
                    }

                }


                if (model.UserId == -1)
                {

                    if(CommonHelper.ExistUserName(model.UserName))
                    {
                        ViewBag.Feed = 2;
                        return View(model);

                    }

                    o.LastLoginDate = DateTime.Now;
                    o.AccessCount = 0;
                    o.TryAccessCount = 0;
                   var userId= svc.AddUser(o);

                    var svcUserRole = new UserRoleAppService();

                    var roles = model.AssignedUserRole.Split(',');
                    foreach (var r in roles)
                    {
                        svcUserRole.AddUserRole(new UserRole { RoleId = Convert.ToInt32(r), UserId = userId });

                    }
                    

                    
                    ViewBag.Feed = 0;
                }
                else
                {
                    o.UserId = model.UserId;
                    o.LastLoginDate = DateTime.Now;
                    if (model.IsDeleteAction == 0)
                    {

                        svc.SaveUser(o);


                        var svcUserRole = new UserRoleAppService();
                        svcUserRole.RemoveUserRoleByUserId(o.UserId);
                        var roles = model.AssignedUserRole.Split(',');
                        foreach (var r in roles)
                        {
                            svcUserRole.AddUserRole(new UserRole { RoleId = Convert.ToInt32(r), UserId = o.UserId });

                        }

                        var eusr = new UserAppService().GetUser(model.UserId);
                        model.UserRoles = eusr.UserRoles.ToList();

                    }
                    else
                    {
                        var svcUserRole = new UserRoleAppService();
                        svcUserRole.RemoveUserRoleByUserId(o.UserId);
                        svc.RemoveUser(model.UserId);
                        model.UserRoles = new List<UserRole>();
                    }
                    ViewBag.Feed = 0;
                }
            }
            catch (Exception)
            {
                ViewBag.Feed = 1;

            }
      
            return View(model);
        }