Esempio n. 1
0
    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="userNode">XML for user</param>
    /// <param name="userIdFixed">NULL if ID is expected in the XML.  If non-NULL we will use the handed in user-id</param>
    private SiteUser(XmlNode userNode, string userIdFixed, SiteUserAuth?siteUserAuthFixed)
    {
        if (userNode.Name.ToLower() != "user")
        {
            AppDiagnostics.Assert(false, "Not a user");
            throw new Exception("Unexpected content - not user");
        }

        //If we have not been handed a user-id, then it must be in the XML
        if (string.IsNullOrEmpty(userIdFixed))
        {
            this.Id = userNode.Attributes["id"].Value;
        }
        else
        {
            this.Id = userIdFixed;
        }

        this.Name     = userNode.Attributes["name"].Value;
        this.SiteRole = userNode.Attributes["siteRole"].Value;

        this.LastLogin       = XmlHelper.GetAttributeDateTimeIfExists(userNode, "lastLogin");
        this.LastLoginAsText = XmlHelper.GetAttributeIfExists(userNode, "lastLogin", null);

        //Not all of the REST APIs return full name
        this.FullName = XmlHelper.GetAttributeIfExists(userNode, "fullName", "");

        this.SiteRoleParsed = ParseUserRole(this.SiteRole);
        AppDiagnostics.Assert(this.SiteRoleParsed != SiteUserRole.Unknown, "Unknown user role: " + this.SiteRole);


        //If we were not passed in an explicit user authentication, then it needs to be in the XML
        if (siteUserAuthFixed == null)
        {
            this.SiteAuthentication = userNode.Attributes["authSetting"].Value;

            this.SiteAuthenticationParsed = ParseUserAuthentication(this.SiteAuthentication);
            AppDiagnostics.Assert(this.SiteAuthenticationParsed != SiteUserAuth.Unknown, "Unknown user auth: " + this.SiteAuthenticationParsed);
        }
        else
        {
            //Use the explicitly passed in value
            this.SiteAuthenticationParsed = siteUserAuthFixed.Value;
            this.SiteAuthentication       = UserAuthenticationToString(this.SiteAuthenticationParsed);
        }


        this.IsSiteAdmin = ParseIsSiteAdmin(this.SiteRole);

        //=============================================================================
        //[2019-10-30] Currently Query User APIs do not return the user's email.
        //If the User Name is the email (as it is in Tableau Online) then grab that
        //=============================================================================
        string candidateEmail = this.Name;

        if (RegExHelper.IsEmail(candidateEmail))
        {
            this.Email = candidateEmail;
        }
    }
Esempio n. 2
0
    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="xmlNode"></param>
    public ProvisioningUser(XmlNode xmlNode, string overrideSourceGroup)
    {
        this.UserAuthentication = xmlNode.Attributes[XmlAttribute_Auth].Value;
        this.UserRole           = xmlNode.Attributes[XmlAttribute_Role].Value;
        this.UserName           = xmlNode.Attributes[XmlAttribute_Name].Value;

        this.RoleRank = CalculateRoleRank(this.UserRole);


        /// <summary>
        /// TRUE: It is not unexpected to find that the user has an acutal role > than this specified role (useful for Grant License on Sign In scenarios)
        /// FALSE: It is unexpected to find the user with a role that differs from this specified role
        /// </summary>
        this.AllowPromotedRole = XmlHelper.ReadBooleanAttribute(xmlNode, XmlAttribute_AllowPromotedRole, false);

        //Source group information (useful for understanding where this role definition came from)
        if (!string.IsNullOrEmpty(overrideSourceGroup))
        {
            this.SourceGroup = overrideSourceGroup;
        }
        else
        {
            var xAttributeSourceGroup = xmlNode.Attributes[XmlAttribute_SourceGroup];
            if (xAttributeSourceGroup != null)
            {
                this.SourceGroup = xAttributeSourceGroup.Value;
            }
        }


        this.UserAuthenticationParsed = SiteUser.ParseUserAuthentication(this.UserAuthentication);
    }
 /// <summary>
 /// Explictly passed in value
 /// </summary>
 /// <param name="userName"></param>
 /// <param name="userRole"></param>
 /// <param name="userAuthentication"></param>
 public ProvisioningUser(string userName, string userRole, string userAuthentication, string sourceGroup)
 {
     this.UserName                 = userName;
     this.UserRole                 = userRole;
     this.UserAuthentication       = userAuthentication;
     this.UserAuthenticationParsed = SiteUser.ParseUserAuthentication(this.UserAuthentication);
     this.RoleRank                 = CalculateRoleRank(userRole);
     this.SourceGroup              = sourceGroup;
 }
Esempio n. 4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="onlineUrls"></param>
 /// <param name="login"></param>
 public SendCreateUser(
     TableauServerUrls onlineUrls,
     TableauServerSignIn login,
     string userName,
     string userRole, //Creator, Explorer, ExplorerCanPublish, SiteAdministratorExplorer, SiteAdministratorCreator, Unlicensed, or Viewer
     SiteUserAuth userAuthentication)
     : base(login)
 {
     _onlineUrls         = onlineUrls;
     _userName           = userName;
     _userRole           = userRole;
     _userAuthentication = userAuthentication;
 }
Esempio n. 5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="onlineUrls"></param>
 /// <param name="login"></param>
 /// <param name="userId">GUID</param>
 /// <param name="newRole">GUID</param>
 public SendUpdateUser(
     TableauServerUrls onlineUrls,
     TableauServerSignIn login,
     string userId,
     string newRole,
     SiteUserAuth newAuthentication)
     : base(login)
 {
     _onlineUrls        = onlineUrls;
     _userId            = userId;
     _newRole           = newRole;
     _newAuthentication = newAuthentication;
 }
Esempio n. 6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="onlineUrls"></param>
 /// <param name="login"></param>
 /// <param name="userId">GUID</param>
 /// <param name="newRole">GUID</param>
 public SendUpdateUser(
     TableauServerSignIn login,
     string userId,
     string newRole,
     bool updateAuthSetting,
     SiteUserAuth newAuthentication)
     : base(login)
 {
     _onlineUrls        = login.ServerUrls;
     _userId            = userId;
     _newRole           = newRole;
     _newAuthentication = newAuthentication;
     _updateAuthSetting = updateAuthSetting;
 }
Esempio n. 7
0
    private SiteUser UpdateUser(string userId, string newRole, SiteUserAuth newAuthentication)
    {
        AppDiagnostics.Assert(!string.IsNullOrWhiteSpace(userId), "missing user id");
        AppDiagnostics.Assert(!string.IsNullOrWhiteSpace(newRole), "missing role");

        string newAuthenticationText = SendCreateUser.SiteUserAuthToAttributeText(newAuthentication);

        //ref: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref.htm#update_user
        var sb        = new StringBuilder();
        var xmlWriter = XmlWriter.Create(sb, XmlHelper.XmlSettingsForWebRequests);

        xmlWriter.WriteStartElement("tsRequest");
        xmlWriter.WriteStartElement("user");
        xmlWriter.WriteAttributeString("siteRole", newRole);
        xmlWriter.WriteAttributeString("authSetting", newAuthenticationText);
        xmlWriter.WriteEndElement(); //</user>
        xmlWriter.WriteEndElement(); // </tsRequest>
        xmlWriter.Close();

        var xmlText = sb.ToString(); //Get the XML text out

        //Create a web request
        var urlUpdateUser = _onlineUrls.Url_UpdateSiteUser(_onlineSession, userId);
        var webRequest    = this.CreateLoggedInWebRequest(urlUpdateUser, "PUT");

        TableauServerRequestBase.SendPutContents(webRequest, xmlText);

        //Get the response
        var response = GetWebReponseLogErrors(webRequest, "update yser (change auth or role)");

        using (response)
        {
            var xmlDoc = GetWebResponseAsXml(response);


            //Get all the user nodes
            var nsManager = XmlHelper.CreateTableauXmlNamespaceManager("iwsOnline");
            var xNodeUser = xmlDoc.SelectSingleNode("//iwsOnline:user", nsManager);

            try
            {
                return(SiteUser.FromUserXMLWithoutUserId(xNodeUser, userId));
            }
            catch (Exception parseXml)
            {
                StatusLog.AddError("Update user, error parsing XML response " + parseXml.Message + "\r\n" + xNodeUser.InnerXml);
                return(null);
            }
        }
    }
Esempio n. 8
0
    /// <summary>
    /// Create the user on the server
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="userRole"></param>
    /// <param name="userAuthentication"></param>
    /// <returns></returns>
    private SiteUser CreateUser(string userName, string userRole, SiteUserAuth userAuthentication)
    {
        string authSettingText = SiteUserAuthToAttributeText(userAuthentication);

        //ref: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref.htm#add_user_to_site
        var sb        = new StringBuilder();
        var xmlWriter = XmlWriter.Create(sb, XmlHelper.XmlSettingsForWebRequests);

        xmlWriter.WriteStartElement("tsRequest");
        xmlWriter.WriteStartElement("user");
        xmlWriter.WriteAttributeString("name", userName);
        xmlWriter.WriteAttributeString("siteRole", userRole);
        xmlWriter.WriteAttributeString("authSetting", authSettingText);
        xmlWriter.WriteEndElement(); //</user>
        xmlWriter.WriteEndElement(); //</tsRequest>
        xmlWriter.Close();

        var xmlText = sb.ToString(); //Get the XML text out

        //Generate the MIME message

        //Create a web request
        var urlCreateUser = _onlineUrls.Url_CreateSiteUser(_onlineSession);
        var webRequest    = this.CreateLoggedInWebRequest(urlCreateUser, "POST");

        TableauServerRequestBase.SendPostContents(webRequest, xmlText);

        //Get the response
        var response = GetWebReponseLogErrors(webRequest, "create user");

        using (response)
        {
            var xmlDoc = GetWebResponseAsXml(response);

            //Get all the workbook nodes
            var nsManager    = XmlHelper.CreateTableauXmlNamespaceManager("iwsOnline");
            var xNodeProject = xmlDoc.SelectSingleNode("//iwsOnline:user", nsManager);

            try
            {
                return(new SiteUser(xNodeProject));
            }
            catch (Exception parseXml)
            {
                StatusLog.AddError("Create user, error parsing XML response " + parseXml.Message + "\r\n" + xNodeProject.InnerXml);
                return(null);
            }
        }
    }
Esempio n. 9
0
    /// <summary>
    /// XML Attribute text from the ennumeration value
    /// </summary>
    /// <param name="userAuthentication"></param>
    /// <returns></returns>
    public static string SiteUserAuthToAttributeText(SiteUserAuth userAuthentication)
    {
        switch (userAuthentication)
        {
        case SiteUserAuth.Default:
            return("ServerDefault");

        case SiteUserAuth.SAML:
            return("SAML");

        default:
            IwsDiagnostics.Assert(false, "810-1036: Unknown auth type for user ");
            throw new Exception("810-1036: Unknown auth type for user ");
        }
    }
Esempio n. 10
0
    /// <summary>
    /// Turn a parsed role into an attribute string
    /// </summary>
    /// <param name="roleName"></param>
    /// <returns></returns>
    public static string UserAuthenticationToString(SiteUserAuth userAuth)
    {
        switch (userAuth)
        {
        case SiteUserAuth.Default:
            return("ServerDefault");

        case SiteUserAuth.SAML:
            return("SAML");

        case SiteUserAuth.OpenID:
            return("OpenId");

        case SiteUserAuth.Unknown:
            return("*Unknown*");

        default:
            IwsDiagnostics.Assert(false, "1018-100: Unrecognized user auth type");
            return("*Unrecognized*");
        }
    }
Esempio n. 11
0
    /// <summary>
    /// Contructor
    /// </summary>
    /// <param name="xmlNode"></param>
    public ProvisioningUser(XmlNode xmlNode, string overrideSourceGroup)
    {
        this.UserAuthentication = xmlNode.Attributes["auth"].Value;
        this.UserRole           = xmlNode.Attributes["role"].Value;
        this.UserName           = xmlNode.Attributes["name"].Value;

        if (!string.IsNullOrEmpty(overrideSourceGroup))
        {
            this.SourceGroup = overrideSourceGroup;
        }
        else
        {
            var xAttributeSourceGroup = xmlNode.Attributes["sourceGroup"];
            if (xAttributeSourceGroup != null)
            {
                this.SourceGroup = xAttributeSourceGroup.Value;
            }
        }


        this.UserAuthenticationParsed = SiteUser.ParseUserAuthentication(this.UserAuthentication);
    }
Esempio n. 12
0
 /// <summary>
 /// Some server reponses do NOT include a user-id (e.g. Update User requests).  For these we
 /// do NOT want to try to look up the user-id.
 /// </summary>
 /// <param name="userNode"></param>
 /// <param name="userIdFixed"></param>
 /// <returns></returns>
 public static SiteUser FromUserXMLWithoutUserIdOrAuthRole(XmlNode userNode, string userIdFixed, SiteUserAuth authRole)
 {
     IwsDiagnostics.Assert(!string.IsNullOrEmpty(userIdFixed), "1018-104: Internal error, expected non-blank user ID");
     return(new SiteUser(userNode, userIdFixed, authRole));
 }