/// <summary> /// Determines if a transition exists between the <paramref name="fromState" /> and /// the <paramref name="toState" /> states for the specified <paramref name="user" />. /// </summary> /// <param name="source">The transitions.</param> /// <param name="user">The user.</param> /// <param name="fromState">The From state.</param> /// <param name="toState">The To state.</param> /// <returns> /// Returns a <see cref="bool" /> representing <c>true</c> if there exists a transition; otherwise, <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException"> /// user /// or /// fromState /// or /// toState /// </exception> public static bool IsValidTransition(this IMMPxTransition source, IMMPxUser user, IMMPxState fromState, IMMPxState toState) { if (source == null) { return(false); } if (user == null) { throw new ArgumentNullException("user"); } if (fromState == null) { throw new ArgumentNullException("fromState"); } if (toState == null) { throw new ArgumentNullException("toState"); } // Compare the from and to states from the enumerations. if (source.FromStates.Contains(fromState) && source.ToStates.Contains(toState)) { // Found a transition with matching To/From states, now check to see // if the user has the proper role assigned to run the transition. if (user.AnyRoleForTransition(source)) { return(true); } } return(false); }
/// <summary> /// Creates an <see cref="IEnumerable{T}" /> from an <see cref="IMMPxTransition" /> /// </summary> /// <param name="source">An <see cref="IMMPxTransition" /> to create an <see cref="IEnumerable{T}" /> from.</param> /// <returns>An <see cref="IEnumerable{T}" /> that contains the fields from the input source.</returns> public static IEnumerable <IMMPxTransition> AsEnumerable(this IMMEnumPxTransition source) { if (source != null) { source.Reset(); IMMPxTransition transition = source.Next(); while (transition != null) { yield return(transition); transition = source.Next(); } } }
/// <summary> /// Determines whether the given <paramref name="source" /> has at least one matching /// role for the <paramref name="transition" />. /// </summary> /// <param name="source">The user.</param> /// <param name="transition">The transition.</param> /// <returns> /// <c>true</c> if the user has at least one matching role with the transition; otherwise, <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException">transition</exception> public static bool AnyRoleForTransition(this IMMPxUser source, IMMPxTransition transition) { if (source == null) { return(false); } if (transition == null) { throw new ArgumentNullException("transition"); } foreach (var userRole in source.Roles.AsEnumerable()) { foreach (var transitionRole in transition.Roles.AsEnumerable()) { if (userRole == transitionRole) { return(true); } } } return(false); }