Пример #1
0
        public void GetBlobEventsByBlob()
        {
            using (TransactionScope ts = new TransactionScope())
            {
                var context = BlobShareDataStoreEntities.CreateInstance();
                var eventService = new EventService(context);
                var user = this.GetTestUser(context);
                var blob = this.GetTestBlob(context);

                eventService.CreateEventUserViewBlob(user.NameIdentifier, user.IdentityProvider, blob);

                var reportService = new ReportService(context);
                int totalPages;

                var events = reportService.GetBlobEventsByBlob(
                    DateTime.UtcNow.AddSeconds(-5),
                    DateTime.UtcNow.AddSeconds(1),
                    new List<int>() { (int)EventType.View },
                    blob.BlobId,
                    100,
                    1,
                    out totalPages);

                Assert.AreEqual(1, totalPages);
                Assert.AreEqual(1, events.Count());
                var e = events.First();
                Assert.AreEqual(blob.BlobId, e.Blob.BlobId);
                Assert.AreEqual(blob.Name, e.Blob.Name);
                Assert.AreEqual((int)EventType.View, e.EventType);
                Assert.AreEqual(user.UserId, e.User.UserId);
            }
        }
Пример #2
0
 public void CreateEventUserLogin()
 {
     using (TransactionScope ts = new TransactionScope())
     {
         BlobShareDataStoreEntities context = BlobShareDataStoreEntities.CreateInstance();
         EventService service = new EventService(context);
         User user = this.GetTestUser(context);
         service.CreateEventUserLogin(user);
         Assert.AreEqual(2, user.UserEvents.Count);
         Assert.AreEqual((int)UserEventType.Login, user.UserEvents.Skip(1).First().EventType);
     }
 }
Пример #3
0
 public void CreateEventUserViewBlob()
 {
     using (TransactionScope ts = new TransactionScope())
     {
         BlobShareDataStoreEntities context = BlobShareDataStoreEntities.CreateInstance();
         EventService service = new EventService(context);
         User user = this.GetTestUser(context);
         Blob blob = this.GetTestBlob(context);
         service.CreateEventUserViewBlob(user.NameIdentifier, user.IdentityProvider, blob);
         Assert.AreEqual(1, user.BlobEvents.Count);
         Assert.AreEqual((int)EventType.View, user.BlobEvents.First().EventType);
     }
 }
        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;
        }
Пример #5
0
        public void GetTopItemsByUser()
        {
            using (TransactionScope ts = new TransactionScope())
            {
                var context = BlobShareDataStoreEntities.CreateInstance();
                var eventService = new EventService(context);
                var user = this.GetTestUser(context);
                var blob = this.GetTestBlob(context);

                eventService.CreateEventUserViewBlob(user.NameIdentifier, user.IdentityProvider, blob);

                var reportService = new ReportService(context);
                var events = reportService.GetTopItemsByUser(user, 3);

                Assert.AreEqual(1, events.Count());
                var e = events.First();
                Assert.AreEqual(1, e.Total);
                Assert.IsFalse(e.IsBlobSet);
                Assert.AreEqual(blob.Name, e.Name);
            }
        }