예제 #1
0
 public void CreateUser()
 {
     using (TransactionScope ts = new TransactionScope())
     {
         UserService service = new UserService();
         User user = new User() { Name = "Test User", Email = "*****@*****.**" };
         service.CreateUser(user);
         Assert.AreNotEqual(Guid.Empty, user.UserId);
     }
 }
예제 #2
0
 private User GetTestUser(BlobShareDataStoreEntities context)
 {
     UserService userService = new UserService(context);
     User user = new User()
     {
         Name = "Test User",
         Email = "*****@*****.**",
         IdentityProvider = "identityProvider",
         NameIdentifier = "nameIdentifier"
     };
     userService.CreateUser(user);
     return user;
 }
예제 #3
0
 public void DeactivateAndActivateUser()
 {
     using (TransactionScope ts = new TransactionScope())
     {
         UserService service = new UserService();
         User user = new User() { Name = "Test User", Email = "*****@*****.**" };
         service.CreateUser(user);
         service.DeactivateUser(user);
         Assert.IsTrue(user.Inactive);
         service.ActivateUser(user);
         Assert.IsFalse(user.Inactive);
         Assert.IsTrue(user.UserEvents.Count == 3);
     }
 }
예제 #4
0
        public void CreateAndRetrieveUserByNameIdentifierAndIdentityProvider()
        {
            using (TransactionScope ts = new TransactionScope())
            {
                UserService service = new UserService();
                User user = new User() { NameIdentifier = "nameIdentifier", IdentityProvider = "identityProvider", Name = "Test Name", Email = "*****@*****.**" };
                service.CreateUser(user);
                User newUser = service.RetrieveUserByNameIdentifier(user.NameIdentifier, user.IdentityProvider);

                Assert.IsNotNull(newUser);
                Assert.AreEqual(user.UserId, newUser.UserId);
                Assert.AreEqual(user.NameIdentifier, newUser.NameIdentifier);
                Assert.AreEqual(user.IdentityProvider, newUser.IdentityProvider);
            }
        }
예제 #5
0
        public void CreateAndRetrieveUserById()
        {
            using (TransactionScope ts = new TransactionScope())
            {
                UserService service = new UserService();
                User user = new User() { Name = "Test User", Email = "*****@*****.**" };
                service.CreateUser(user);
                User newUser = service.RetrieveUserById(user.UserId);

                Assert.IsNotNull(newUser);
                Assert.AreEqual(user.UserId, newUser.UserId);
                Assert.AreEqual(user.Name, newUser.Name);
                Assert.AreEqual(user.Email, newUser.Email);
            }
        }
        private static User EnsureApplicationUser(IClaimsIdentity identity)
        {
            var invitationService = new InvitationService();
            var context = BlobShareDataStoreEntities.CreateInstance();
            var userService = new UserService(context);

            var invitationId = Guid.Empty;
            User user = null;

            // Get name identifier and identity provider
            var nameIdentifierClaim = identity.Claims.Where(c => c.ClaimType.Equals(ClaimTypes.NameIdentifier, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
            var identityProviderClaim = identity.Claims.Where(c => c.ClaimType.Equals(IdentityProviderClaimType, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();

            // Administrator First Login
            if (userService.GetUsers().Count() == 0)
            {
                var emailClaim = identity.Claims.Where(c => c.ClaimType.Equals(ClaimTypes.Email, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();

                return new User()
                {
                    NameIdentifier = nameIdentifierClaim.Value,
                    IdentityProvider = identityProviderClaim.Value,
                    Email = emailClaim == null ? string.Empty : emailClaim.ToString(),
                    Name = emailClaim == null ? string.Empty : emailClaim.ToString(),
                };
            }

            if (IsInvitationRequest(out invitationId) && !string.IsNullOrWhiteSpace(nameIdentifierClaim.Value))
            {
                // TODO: Lock the activation process
                var invitation = invitationService.RetrieveInvitation(invitationId);

                if (invitation == null)
                {
                    throw new InvalidInvitationException("Invalid Invitation ID", "It seems that the provided invitation ID does not exist. Please contact your administrator.");
                }

                if (invitation.ActivationDateTime.HasValue)
                {
                    throw new InvalidInvitationException("Invitation Already Activated", "It seems that the invitation was already activated. Please contact your administrator.");
                }

                if (DateTime.UtcNow.CompareTo(invitation.ExpirationDateTime) > 0)
                {
                    throw new InvalidInvitationException("Invitation Expired", "It seems that the invitation you are trying to activate has already expired. Please contact your administrator.");
                }

                user = userService.RetrieveUserByNameIdentifier(nameIdentifierClaim.Value, identityProviderClaim.Value);

                if (user != null)
                {
                    if (!user.Email.Equals(invitation.Email))
                    {
                        throw new InvalidUserException("Account Already Linked", "It seems that you have already linked this account with another user. Please try again with a different one.");
                    }
                }
                else
                {
                    user = userService.RetrieveUserById(invitation.User.UserId);
                }

                if (user == null)
                {
                    user = new User()
                    {
                        Name = invitation.Email,
                        Email = invitation.Email,
                        NameIdentifier = nameIdentifierClaim.Value,
                        IdentityProvider = identityProviderClaim.Value
                    };

                    userService.CreateUser(user);
                }
                else
                {
                    if (user.NameIdentifier != nameIdentifierClaim.Value || user.IdentityProvider != identityProviderClaim.Value)
                    {
                        user.NameIdentifier = nameIdentifierClaim.Value;
                        user.IdentityProvider = identityProviderClaim.Value;

                        userService.UpdateUser(user);
                    }
                }

                invitationService.ActivateUserInvitation(invitation, user);
            }
            else
            {
                user = userService.RetrieveUserByNameIdentifier(nameIdentifierClaim.Value, identityProviderClaim.Value);

                if (user == null)
                {
                    throw new InvalidUserException("Invalid User", "It seems that no user is linked to this account, please try again with another or contact your administrator.");
                }
            }

            if (user.Inactive)
            {
                throw new InvalidUserException("Inactive User", "It seems that this user was deactivated, Please contact your administrator.");
            }

            var eventService = new EventService(context);
            eventService.CreateEventUserLogin(user);

            return user;
        }
예제 #7
0
        public void GetUsers()
        {
            using (TransactionScope ts = new TransactionScope())
            {
                UserService service = new UserService();

                for (int k = 1; k <= 10; k++)
                {
                    User user = new User()
                    {
                        Name = string.Format("Test User {0}", k),
                        Email = string.Format("test{0}@live.com", k)
                    };
                    service.CreateUser(user);
                }

                var users = service.GetUsers();

                Assert.IsNotNull(users);
                Assert.IsTrue(users.Count() >= 10);
            }
        }
예제 #8
0
        public void SearchUser()
        {
            using (TransactionScope ts = new TransactionScope())
            {
                UserService service = new UserService();
                service.CreateUser(new User() { Name = "Test User", Email = "*****@*****.**" });
                service.CreateUser(new User() { Name = "YesTest User", Email = "*****@*****.**" });
                service.CreateUser(new User() { Name = "Non User", Email = "*****@*****.**" });

                var users = service.SearchUsers("Test");

                Assert.IsNotNull(users);
                Assert.AreEqual(2, users.Count());
            }
        }