Direct Users to the Dashboard Upon Login

Topics: Customizing Orchard
Oct 29, 2014 at 5:47 PM
Is it possible to send users who have permission to access the dashboard straight to the dashboard upon login instead of remaining on the homepage? Maybe through a custom workflow activity or something? Thank you.
Oct 29, 2014 at 8:34 PM
Edited Oct 29, 2014 at 9:07 PM
Hi!

I am not sure if it is working or not. But I would Implement the "IUserEventHandler" from Orchard.Users. In the EventHandler the "LoggedIn" Method should be your entrypoint. This event is fired if the user logged in successfully. You can manipulate in these event the request. Change the "returnUrl" Parameter to the url you want the user gets redirected.

edit: Added pseudo code - DON'T copy one to one
using System.Web;
using NGM.OpenAuthentication.Services;
using Orchard.Mvc;
using Orchard.Security;
using Orchard.Users.Events;

namespace NGM.OpenAuthentication.Events {
    public class MyUserEventHandler : IUserEventHandler {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IAuthorizationService _authorizationService;

        public OpenAuthUserEventHandler(IHttpContextAccessor httpContextAccessor, IAuthorizationService authorizationService) {
            _httpContextAccessor = httpContextAccessor;
            _authorizationService = authorizationService;
        }

       
        public void LoggedIn(IUser user) {
            var current = _httpContextAccessor.Current();
            
            if(current == null)
                return;
                
            var request = current.Request;

            if (request == null)
                return; 
                
            if(_authorizationService.CheckAccess(Permission.AccessAdminPanel, user, null)
                request["returnUrl"] = ~/Admin; // This should be a little cleaner, I think
        }
    }
}
Oct 29, 2014 at 8:49 PM
look at sipke's demo from orchard harvest 2014 on workflows. it deoes exactly that.
Nov 3, 2014 at 5:00 PM
I tried using the above method but wasn't able to get it working as a workflow activity. I looked at the Orchard.Users module and was trying to emulate the way it determines if there was a login but that doesn't appear to be working either. Here is the code for my activity.

namespace Orchard.ExtendedWorkflows.Activities {
public class LoginActivity : Event {
    private readonly IWorkContextAccessor _workContextAccessor;
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly IAuthorizationService _authorizationService;
    private readonly IAuthenticationService _authenticationService;

    public LoginActivity(IWorkContextAccessor workContextAccessor, IHttpContextAccessor httpContextAccessor, IAuthorizationService authorizationService, IAuthenticationService authenticationService) {
        _workContextAccessor = workContextAccessor;
        _httpContextAccessor = httpContextAccessor;
        _authorizationService = authorizationService;
        _authenticationService = authenticationService;
        T = NullLocalizer.Instance;
    }

    public override bool CanStartWorkflow
    {
        get { return true; }
    }

    public Localizer T { get; set; }

    public override string Name {
        get { return "Login"; }
    }

    public override LocalizedString Category {
        get { return T("Conditions"); }
    }

    public override LocalizedString Description {
        get { return T("Waits for user to login.");  }
    }

    public override string Form {
        get { return "SelectRoles"; }
    }

    public override IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext) {
        return new[] {T("Login"), T("No Login")};
    }

    public override bool CanExecute(WorkflowContext workflowContext, ActivityContext activityContext) {       
        return true;
    }

    public override IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext) {

        if (isLogin() && UserIsInRole(activityContext))
        {
            yield return T("Login");
        }
        else
        {
            yield return T("No Login");
        }
    }

    public bool isLogin() {
        if (_authenticationService.GetAuthenticatedUser() != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private bool UserIsInRole(ActivityContext context) {

        // checking if user is in an accepted role
        var workContext = _workContextAccessor.GetContext();
        var user = workContext.CurrentUser;
        var roles = GetRoles(context);

        return UserIsInRole(user, roles);
    }

    public static bool UserIsInRole(IUser user, IEnumerable<string> roles) {
         bool isInRole = false;

        if (user == null) {
            isInRole = roles.Contains("Anonymous");
        }
        else {
            dynamic dynUser = user.ContentItem;

            if (dynUser.UserRolesPart != null) {
                IEnumerable<string> userRoles = dynUser.UserRolesPart.Roles;
                isInRole = userRoles.Any(roles.Contains);
            }
        }

        return isInRole;
    }

    private IEnumerable<string> GetRoles(ActivityContext context) {
        string roles = context.GetState<string>("Roles");

        if (String.IsNullOrEmpty(roles)) {
            return Enumerable.Empty<string>();
        }

        return roles.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
    }
}
}
Nov 3, 2014 at 6:07 PM
Edited Nov 3, 2014 at 7:02 PM
Hi!
This activity is implemented in 1.x. Take a look at https://orchard.codeplex.com/SourceControl/latest#src/Orchard.Web/Modules/Orchard.Roles/Activities/AssignRoleActivity.cs

Edit: The role activity also there in 1.8. You have to activate the "Roles Workflows Activities" Feature
Nov 4, 2014 at 4:09 PM
Sorry, I'm trying to create a login activity that fires when a user in the specified role logs into the website. I know about the various role activities, I grabbed the is in role activity and am using that to make my login activity. I'm having a hard time figuring out how to capture whether or not a user has logged in. I know the LoggedIn event is fired whenever a user logs in, but I can't figure out how to capture whether or not the event has been fired so I can get the activity to start running. Any ideas? Thank you.
Nov 4, 2014 at 7:31 PM
I am sorry, I posted my solution to the wrong post: https://orchard.codeplex.com/discussions/571279