How to use the roles in WWF?

first,

I add a roles in the "statemachine"->"state"->"HandleExternalEvent".

now I want to do the following things:

In the application form which uses above statemachine workflow, how to check if the login users is part of the roles

then,

if it's true than do the event.

Now i know that WWF have the "System.Workflow.Activities.WorkflowRols" and "WorkflowRoleCollection()", but i don't know how to use it , and i can't fine the example about "roles",

please tell me how to use the roles in WWF,thanks.




Answer this question

How to use the roles in WWF?

  • marvinbaby

    Here's a simple sample of a custom workflow role where you provid the name and add the members at runtime.

    internal class CustomWorkflowRole : WorkflowRole
    {
    List<string> identities = new List<string>();
    private string roleName;

    internal CustomWorkflowRole(string name) : base()
    {
    roleName = name;
    }

    public void AddIdentity(string identity)
    {
    identities.Add(identity);
    }


    public override IList<string> GetIdentities()
    {
    return identities as IList<string>;
    }

    public override bool IncludesIdentity(string identity)
    {
    return identities.Contains(identity);
    }

    public override string Name
    {
    get
    {
    return roleName;
    }
    set
    {
    roleName = value;
    }
    }
    }

    You need to call AddIdentity for each user name you want in the role. You'll need to make sure the identity on the event args passed in matches one of these names in order for the call to succeed.

    Matt



  • Richard Highton

    There are two Roles samples under Technologies\Roles in the Samples.zip file which can be found at %ProgramFiles%\Microsoft SDKs\Windows Workflow Foundation.



  • Johannes Jauch

    thanks, Matt.

    I do it like this:

    public class SouthRole:WorkflowRole
    {
    private string connectionString = "User ID = test; Data Source = SOUTHGIS; Password = test;";
    private string roleName;

    public SouthRole(string roleName)
    {
    this.Name = roleName;
    }

    public override IList<string> GetIdentities()
    {
    OracleConnection roleConnection = new OracleConnection(connectionString);
    roleConnection.Open();
    string roleSQLText = "Select Users.UserId, Users.UserName as UserName from Users,Departments, UsersInDepartments where Users.UserId = UsersInDepartments.UserId and Departments.DepartmentId = UsersInDepartments.DepartmentId and Departments.DepartmentName ='" + this.Name + "'";
    OracleCommand roleCommand = new OracleCommand(roleSQLText, roleConnection);
    OracleDataReader roleReader;
    roleReader = roleCommand.ExecuteReader();
    List<string> list = new List<string>();
    while (roleReader.Read())
    {
    list.Add(roleReader["UserName"].ToString());
    }

    roleReader.Close();
    roleConnection.Close();
    return list;
    }

    public override bool IncludesIdentity(string identity)
    {
    bool isIncludesIdentity = false;
    OracleConnection roleConnection = new OracleConnection(connectionString);
    roleConnection.Open();

    string roleSQLText = "Select UsersInDepartments.UserId, UsersInDepartments.DepartmentId from Users, Departments, UsersInDepartments where Users.UserId = UsersInDepartments.UserId and Users.UserName = '" + identity + "' and Departments.DepartmentId = UsersInDepartments.DepartmentId and Departments.DepartmentName ='" + this.Name + "'";
    OracleCommand roleCommand = new OracleCommand(roleSQLText, roleConnection);
    OracleDataReader roleReader;
    roleReader = roleCommand.ExecuteReader();
    if (roleReader.Read())
    {
    isIncludesIdentity = true;
    }
    else
    {
    isIncludesIdentity = false;
    }
    roleReader.Close();
    roleConnection.Close();
    return isIncludesIdentity;
    }

    public override string Name
    {
    get
    {
    return roleName;
    }
    set
    {
    roleName = value;
    }
    }
    }



  • Karthikeyan

    Looks good, although I'd probably add some logic to make sure I only go to the database once instead of twice. You already have the list of users in the role from the one call, why not keep that in a local variable and then use that local list for other calls. I'd create a helper method that loads the roles and flips a boolean indicating that they have been loaded. Then in my IncludesIdenity and GetIdentities, I'd make sure they have been loaded, then do all my logic against the list.

    Matt



  • Len80

    Just derive from the abstract class System.Workflow.Activities.WorkflowRole and implement the two methods and one property.



  • Zeeshan Umar

    thanks, Tom.

    But how to implement the two methods and one property.

    can you give me one sample or tell me where i can get the samples thanks!



  • Freezer

    hi, Tom.

    thanks your answer.

    but i want to define roles by myself, I don't want to use the ActiveDirectoryRoles and the WebWorkflowRole.

    how can I do thanks!



  • How to use the roles in WWF?