/// <summary>
        /// Checks if specified operation is allowed for user
        /// </summary>
        /// <param name="operation">Operation to check</param>
        public void CheckAccess(AuthOperation operation)
        {
            var allowedOperations = GetAllUserOperations();

            if (!allowedOperations.Contains(operation))
            {
                throw new AuthorizationException();
            }
        }
        /// <summary>
        /// Returns an instance of <see cref="AllowedOperations"/> class, which allows to check permissions
        /// to specified operation
        /// </summary>
        /// <param name="requestedOperation">Operation to check</param>
        /// <returns>An instance of <see cref="AllowedOperations"/> class</returns>
        public AllowedOperations GetAllowedOperations(AuthOperation requestedOperation)
        {
            if (requestedOperation == null)
            {
                throw new ArgumentNullException(nameof(requestedOperation), "Requested operation shouldn't be null!");
            }

            return(GetAllowedOperations(new List <AuthOperation> {
                requestedOperation
            }));
        }
        public void AuthOperation_SameAreaIdDifferentOperationId_OperationsNotEquals()
        {
            // Arrange
            AuthOperation operation1 = Tuple.Create(AREA_1_ID, OPERATION_1_ID);
            AuthOperation operation2 = Tuple.Create(AREA_1_ID, OPERATION_2_ID);

            // Act
            bool result = operation1 == operation2;

            // Assert
            Assert.IsFalse(result);
        }
        public void AuthOperation_DifferenAreaIdSameOperationId_OperationsNotEquals()
        {
            // Arrange
            AuthOperation operation1 = Tuple.Create(AREA_1_ID, OPERATION_1_ID);
            AuthOperation operation2 = Tuple.Create(AREA_2_ID, OPERATION_1_ID);

            // Act
            var result = operation1 == operation2;

            // Assert
            Assert.False(result);
        }
        public void AuthOperation_SameAreaIdOperationId_OperationsEquals()
        {
            // Arrange
            AuthOperation operation1 = Tuple.Create(AREA_1_ID, OPERATION_1_ID);
            AuthOperation operation2 = Tuple.Create(AREA_1_ID, OPERATION_1_ID);

            // Act
            var result = operation1 == operation2;

            // Assert
            Assert.True(result);
        }
        public void GetAllowedOperations_NullSpecified_ArgumentNullExceptionThrown()
        {
            // Arrange
            var allAllowedOperations = new List <AuthOperation>()
            {
                Tuple.Create(AREA_1_ID, OPERATION_1_ID),
                Tuple.Create(AREA_1_ID, OPERATION_2_ID)
            };

            MockAllowedOperations(allAllowedOperations);

            AuthOperation requestedOperations = null;
            var           service             = BuildSUT();

            // Act
            var allowedOperations = service.GetAllowedOperations(requestedOperations);
        }
 private void VerifyCheckAccess(AuthOperation operation, Times times)
 {
     _authServiceMock.Verify(tr => tr.CheckAccess(operation), times);
 }
 private void MockAuthServiceThrownException(AuthOperation operation)
 {
     _authServiceMock.Setup(tr => tr.CheckAccess(operation)).Throws <AuthorizationException>();
 }
Esempio n. 9
0
 private void VerifyGetAllowedOperation(AuthOperation allowedOperation, Times times)
 {
     _authServiceMock.Verify(tr => tr.GetAllowedOperations(allowedOperation), times);
 }
        public static async Task SendTokenAsync(this IEmailSender emailSender, string email, AuthOperation messageKind, string link, string token)
        {
            var subject = "";
            var message = "";

            switch (messageKind)
            {
            case AuthOperation.AddingOtherUserEmail:
                subject = "Adding email failed.";
                message = $"The email '{email}' is already in use by another account! <br/>" +
                          $"Try logging out then logging in with that email instead. <br/>" +
                          $"If you still need to add it to this account then delete the other one.";
                break;

            case AuthOperation.AddingNovelEmail:
                subject = "Add this email to your account";
                message = $"To add this email to your account, please <a href='{link}'>click here</a>. <br/>" +
                          $"Alternatively, use this code: {token}.";
                break;

            case AuthOperation.Registering:
                subject = "Create your account";
                message = $"To continue creating your account, please <a href='{link}'>click here</a>. <br/>" +
                          $"Alternatively, use this code: {token}.";
                break;

            case AuthOperation.LoggingIn:
                subject = "Login to your account";
                message = $"To login to your account, please <a href='{link}'>click here</a>. <br/>" +
                          $"Alternatively, use this code: {token}.";
                break;

            default:
                break;
            }

            await emailSender.SendEmailAsync(email, subject, message);
        }
Esempio n. 11
0
 /// <summary>
 /// Returns the flag - is specified operation is allowed for user
 /// </summary>
 /// <param name="operation">Operation to check</param>
 /// <returns>Sign if operation is allowed</returns>
 public bool IsAllowed(AuthOperation operation)
 {
     return(_allowedOperations.Contains(operation));
 }