예제 #1
0
 public void Register(User user)
 {
     using (var db = base.NewDB())
     {
         if (db.Users.Get(user.Username) != null)
         {
             throw new KnownException("username already registered.");
         }
         user.NewId();
         user.Password = CryptoService.MD5Encrypt(user.Password);
         db.Users.Add(user);
         db.SaveChanges();
     }
 }
예제 #2
0
 public void Save(User user, string[] roles)
 {
     using (var db = base.NewDB())
     {
         if (user.IsNew)
         {
             if (db.Users.Duplicates(null, user.Username))
             {
                 throw new KnownException("用户名已经存在了。");
             }
             user.NewId();
             user.Password = CryptoService.MD5Encrypt(user.Password);
             if (roles != null && roles.Length > 0)
             {
                 var dbRoles = db.Roles.Where(x => roles.Contains(x.Name)).ToList();
                 user.UserRoleRls = new List<UserRoleRL>();
                 foreach (var role in dbRoles)
                 {
                     db.UserRoleRls.Add(new UserRoleRL(user.Id, role.Id));
                 }
             }
             db.Users.Add(user);
         }
         else
         {
             if (db.Users.Duplicates(user.Id, user.Username))
             {
                 throw new KnownException("用户名已经存在了。");
             }
             var dbUser = db.Users.Get(user.Id);
             dbUser.Update(user);
             if (!string.IsNullOrWhiteSpace(user.Password))
             {
                 dbUser.Password = CryptoService.MD5Encrypt(user.Password);
             }
             dbUser.UserRoleRls.ToList().ForEach(x => db.UserRoleRls.Remove(x));
             if (roles != null && roles.Length > 0)
             {
                 var dbRoles = db.Roles.Where(x => roles.Contains(x.Name)).ToList();
                 foreach (var role in dbRoles)
                 {
                     db.UserRoleRls.Add(new UserRoleRL(user.Id, role.Id));
                 }
             }
         }
         db.SaveChanges();
     }
 }