예제 #1
0
 public ActionResult Create(UserFormViewModelBase input,string[] UserRoles)
 {
     bool nameUsed = _repository.FindByName(input.UserName) != null;
     if (nameUsed) ModelState.AddModelError("用户名", "该用户名已经有人占用");
     if (UserRoles==null) ModelState.AddModelError("角色","必须为用户选择角色");
     if (ModelState.IsValid)
     {
         var user = new User();
         user.UserName = input.UserName;
         user.LoginName = input.LoginName;
         user.DepartMentId = input.DepartMentId;
         user.Password = string.IsNullOrEmpty(input.Password)? "123456":input.Password;
         user.Activated = input.Activated;
         user.Roles = new List<Role>();
         foreach (var role in UserRoles)
         {
             var userrole = _roleRepository.Find(Convert.ToInt32(role));
             user.Roles.Add(userrole);
         }
         _repository.Add(user);
         UnitOfWork.Commit();
         return RedirectToAction("Manage", "Account");
     }
     input.AvailableRoles = _roleRepository.FindAll().ToList();
     ViewData["Departs"] = _departRepository.FindAll().ToList();
     return View(input);
 }
예제 #2
0
 /// <summary>
 /// 列出当前登录用户该项目的节点任务
 /// </summary>
 /// <param name="currentuser"></param>
 /// <returns></returns>
 public string[] GetCurrentLogByUser(User currentuser)
 {
     return (from
                TaskLog log in TaskLogs.Where(x => x.IsActive() && currentuser.IsInRole(x.TaskNode.DutyRole.Name))
                 select log.TaskNode.TaskName
                ).ToArray();
 }
예제 #3
0
 protected internal void LogOn(User user, bool rememberMe)
 {
     var principal = new GenericPrincipal(new GenericIdentity(user.UserName), user.GetRoles());
     if (System.Web.HttpContext.Current != null)
     {
         DateTime expires = DateTime.Now.AddHours(1);
         var ticket = new FormsAuthenticationTicket(
             1,                        //version
             user.UserName,            //user name
             DateTime.Now,             //creation
             expires,                  //Expiration
             rememberMe,               //Persistent
             String.Join(",", user.GetRoles())
         );
         string encryptedTicket = FormsAuthentication.Encrypt(ticket);
         var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
         if (ticket.IsPersistent) authCookie.Expires = expires;
         Response.Cookies.Add(authCookie);
         //set the session variables
         Session["Principal"] = principal;
         Session["User"] = user;
     }
     else
     {
         Thread.CurrentPrincipal = principal;
     }
 }
예제 #4
0
 public MyAccountFormViewModel(User user )
 {
     UserId = user.UserId;
     UserName = user.UserName;
     LoginName = user.LoginName;
 }