/// <summary>
            /// Executes the workflow to do user authentication.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <returns>The response.</returns>
            protected override UserAuthenticationResponse Process(UserAuthenticationRequest request)
            {
                ThrowIf.Null(request, "request");
                Device           device = null;
                CommerceIdentity identity;
                Employee         employee;
                string           deviceId = string.IsNullOrWhiteSpace(request.DeviceId)
                    ? request.RequestContext.GetPrincipal().DeviceNumber
                    : request.DeviceId;

                string deviceToken = string.IsNullOrWhiteSpace(request.DeviceToken)
                    ? request.RequestContext.GetPrincipal().DeviceToken
                    : request.DeviceToken;

                try
                {
                    // Authenticate device only when the device token is specified
                    if (!string.IsNullOrWhiteSpace(deviceToken))
                    {
                        device = AuthenticationHelper.AuthenticateDevice(
                            this.Context,
                            deviceToken);
                    }

                    // User logs on.
                    employee = AuthenticationHelper.AuthenticateAndAuthorizeUser(request, device);

                    identity = new CommerceIdentity(employee, device);

                    // If the request is for elevate operation
                    if (request.RetailOperation != RetailOperation.None)
                    {
                        // Add the Elevation properties to the claim.
                        identity.OriginalUserId          = this.Context.GetPrincipal().UserId;
                        identity.ElevatedRetailOperation = request.RetailOperation;

                        // successful manager override for operation with id and operator with id
                        var message = string.Format(
                            "Manager with id '{0}' has approved override for operation with id '{1}' to the operator with id '{2}'.",
                            request.StaffId,
                            identity.ElevatedRetailOperation,
                            identity.OriginalUserId);
                        LogAuditEntry(request.RequestContext, "ElevateUser", message);
                    }

                    return(new UserAuthenticationResponse(employee, device, identity));
                }
                catch (Exception exception)
                {
                    RetailLogger.Log.CrtWorkflowUserAuthenticationRequestHandlerFailure(request.StaffId, deviceId, exception);
                    throw;
                }
            }
            /// <summary>
            /// Executes the workflow to get available stores.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <returns>The response.</returns>
            protected override GetAvailableStoresResponse Process(GetAvailableStoresRequest request)
            {
                ThrowIf.Null(request, "request");

                // Validate device token
                AuthenticationHelper.AuthenticateDevice(this.Context, request.DeviceToken);

                var getAllStoresDataRequest = new SearchOrgUnitDataRequest(new List <string>(), QueryResultSettings.AllRecords);
                var stores = this.Context.Runtime.Execute <EntityDataServiceResponse <OrgUnit> >(getAllStoresDataRequest, this.Context).PagedEntityCollection;

                var response = new GetAvailableStoresResponse(stores);

                return(response);
            }
            /// <summary>
            /// Executes the workflow to unlock a register.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <returns>The response.</returns>
            protected override NullResponse Process(UnlockRegisterRequest request)
            {
                ThrowIf.Null(request, "request");
                Device device = null;

                ICommercePrincipal principal = this.Context.GetPrincipal();
                string             userId    = principal.UserId;

                try
                {
                    if (userId != request.StaffId)
                    {
                        throw new UserAuthenticationException(SecurityErrors.Microsoft_Dynamics_Commerce_Runtime_UnlockRegisterFailed);
                    }

                    if (request.DeviceId != null && request.DeviceToken != null)
                    {
                        // Authenticate device only when DeviceId is specified
                        device = AuthenticationHelper.AuthenticateDevice(
                            this.Context,
                            request.DeviceToken);
                    }

                    // Unlock the terminal
                    AuthenticationHelper.UnlockRegister(this.Context, device, request);
                    return(new NullResponse());
                }
                catch (DeviceAuthenticationException ex)
                {
                    RetailLogger.Log.CrtWorkflowUnlockRegisterRequestHandlerFailure(request.StaffId, request.DeviceId, ex);
                    throw;
                }
                catch (UserAuthenticationException ex)
                {
                    RetailLogger.Log.CrtWorkflowUnlockRegisterRequestHandlerFailure(request.StaffId, request.DeviceId, ex);
                    throw new UserAuthenticationException(SecurityErrors.Microsoft_Dynamics_Commerce_Runtime_UnlockRegisterFailed);
                }
                catch (Exception ex)
                {
                    RetailLogger.Log.CrtWorkflowUnlockRegisterRequestHandlerFailure(request.StaffId, request.DeviceId, ex);
                    throw new UserAuthenticationException(SecurityErrors.Microsoft_Dynamics_Commerce_Runtime_AuthenticationFailed);
                }
            }
Пример #4
0
            /// <summary>
            /// Executes the workflow to do user authentication renewal.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <returns>The response.</returns>
            protected override UserAuthenticationRenewalResponse Process(UserAuthenticationRenewalRequest request)
            {
                ThrowIf.Null(request, "request");
                Device device = null;

                // If device Id is present, authenticate the device to check if the device is active.
                if (!string.IsNullOrWhiteSpace(this.Context.GetPrincipal().DeviceNumber))
                {
                    device = AuthenticationHelper.AuthenticateDevice(this.Context, this.Context.GetPrincipal().DeviceToken);
                }

                // Send authentication renewal request to the service.
                Employee employee = AuthenticationHelper.AuthenticateRenewalUser(this.Context, device);

                CommerceIdentity identity = new CommerceIdentity(employee, device)
                {
                    // Add the LogOn Configuration to the claim.
                    LogOnConfiguration = this.Context.GetPrincipal().LogOnConfiguration
                };

                return(new UserAuthenticationRenewalResponse(employee, device, identity));
            }