Esempio n. 1
0
 public override void CreateRole(string roleName)
 {
     var newRole = new ORM.Roles() { Name = roleName };
     using (var context = new AlbumDbEntities())
     {
         context.Roles.Add(newRole);
         context.SaveChanges();
     }
 }
        //private IUserRepository userRepository;//IAccountService!!!
        public MembershipUser CreateUser(string email, string password)
        {
            //userRepository = (IUserRepository)DependencyResolver.Current.GetService(typeof(IUserRepository));

            MembershipUser membershipUser = GetUser(email, false);

            if (membershipUser != null)
            {
                return null;
            }

            using (var context = new AlbumDbEntities())
            {
                var user = new Users
                {
                    Email = email,
                    Password = Crypto.HashPassword(password),
                    //http://msdn.microsoft.com/ru-ru/library/system.web.helpers.crypto(v=vs.111).aspx
                };

                var role = context.Roles.FirstOrDefault(r => r.Name == "user");
                if (role != null)
                {
                    user.Roles.Add(role);
                }
                if (!context.Users.Any())
                {
                    var adminRole = context.Roles.FirstOrDefault(r => r.Name == "admin");
                    if (adminRole != null)
                    {
                        user.Roles.Add(adminRole);
                    }
                }

                user.Name = "name";

                context.Users.Add(user);
                context.SaveChanges();
                membershipUser = GetUser(user.Email, false);
                return membershipUser;
            }
        }