Example

This page follows the previous section's example and sets the rights, roles, and inheritance options for a user on an object.

<%@ Language=JavaScript%>

<!-- #include file="RetrieveIStore.csp" -->

<!-- #include file=helper_js.csp -->

<!-- #include file=ceconstjs.inc -->

<html>

<body>

<%


function SetInheritance(IStore, UserID, ReportID) {


    Reports = IStore.Query("Select SI_ID From CI_INFOOBJECTS Where SI_ID="+ReportID);

    Report = Reports.Item(1);

    SecurityInfo = Report.SecurityInfo;

    

    // Get the ObjectPrincipal object, or create it by adding the user

    // to the collection.

    try {

        Principal = SecurityInfo.ObjectPrincipals.Item("#"+UserID);

    }

    catch(e) {

        Principal = SecurityInfo.ObjectPrincipals.Add(UserID);

    }

    

    // Force advanced rights.

    Principal.Role = ceRoleAdvanced;

    

    // Set the inheritance flags according to the user's selections.

    if (Request.Form.Item("InheritFromFolders") == "on") {

        Principal.AdvancedInheritFolders = true;

    } else {

        Principal.AdvancedInheritFolders = false;

    }

    

    if (Request.Form.Item("InheritFromGroups") == "on") {

        Principal.AdvancedInheritGroups = true;

    } else {

        Principal.AdvancedInheritGroups = false;

    }

    

    IStore.Commit(Reports);

    

}


function SetRole(IStore, UserID, ReportID)

{

    var Principal;

    

    Reports = IStore.Query("Select SI_ID From CI_INFOOBJECTS Where SI_ID="+ReportID);

    Report = Reports.Item(1);

    SecurityInfo = Report.SecurityInfo;

    

    // Get the ObjectPrincipal object, or create it by adding the user

    // to the collection.

    try {

        Principal = SecurityInfo.ObjectPrincipals.Item("#"+UserID);

    }

    catch(e) {

        Principal = SecurityInfo.ObjectPrincipals.Add(UserID);

    }

    

    Principal.Inherited = false;


    //Assign the new role according to the user's selection.

    var NewRole = parseInt(Request.Form.Item("Role"));

    Principal.Role = NewRole;

    

    IStore.Commit(Reports);


}


function SetRights(IStore, UserID, ReportID)

{

    Reports = IStore.Query("Select TOP 1* From CI_INFOOBJECTS Where SI_ID="+ReportID);

    Report = Reports.Item(1);

    SecurityInfo = Report.SecurityInfo;

    

    try {

        Principal =  SecurityInfo.ObjectPrincipals.Item("#" + UserID);

    }

    catch(e) {

        Principal = SecurityInfo.ObjectPrincipals.Add(UserID);

    }


    // Force Advanced rights in order to set rights explicitly.

    Principal.Role = ceRoleAdvanced;

    

    for (k=1;k<=SecurityInfo.KnownRights.Count;k++)

    {

        // Get the ID of each right that is possible for the report object.

        RightID = SecurityInfo.KnownRights.Item(k).ID;


        try {

            PrincipalRight = Principal.Rights.Item("#" + RightID);

        } catch(e) {

            // If the try block failed, the right is inherited or "Not Specified".

            // Add such rights to the explicit rights collection, even if they will be removed below.

            PrincipalRight = Principal.Rights.Add(RightID);

        }


        try

        {

            // Grant, deny, or delete the right according to the user's selection.

            switch (String(Request.Form.Item(String(RightID))))

            {

            case "granted":

                PrincipalRight.Granted=true;    

                break;

            case "denied":

                PrincipalRight.Granted=false;    

                break;

            case "inherited":

                Principal.Rights.Delete("#" + RightID);

                break;

            case "notspecified":

                Principal.Rights.Delete("#" + RightID);

                break;

            }

        }

        catch(e)

        {

            Response.Write("Could not set the right:" +  SecurityInfo.KnownRights.Item(k).Description + " " + e.description + "<BR>");

        }                    


    }

    IStore.Commit(Reports);    

}

    

function Main()

{

    IStore = RetrieveIStore();

    if (IStore == null)

    {

        Response.Redirect ("Start.csp");

        return;

    }

    UserID = parseInt(Request.QueryString.Item("UserID"));

    ReportID = parseInt(Request.QueryString.Item("ReportID"));

    Response.Write ("<H2>Setting rights for this user</H2><HR>");


    // Call the function appropriate to the user's action.

    if (Request.Form.Item("SetRole") == "Set") SetRole(IStore, UserID, ReportID);

    if (Request.Form.Item("SetRights") == "Set") SetRights(IStore, UserID, ReportID);

    if (Request.Form.Item("SetInheritance") == "Set") SetInheritance(IStore, UserID, ReportID);

    

    Response.Write("Done<BR>");

    

    Response.Write ("<form action='ViewRights.csp' method=post name='Rights'>");

    Response.Write("<input type=hidden name='UserID' value='" + UserID + "'>");

    Response.Write("<input type=hidden name='ReportID' value='" + ReportID + "'>");

    Response.Write ("<input type=submit name='Back' value='View changes'>");

    Response.Write ("</FORM>");

    

}


Main();

    

%>

</body>

</html>





Crystal Decisions, Inc.
http://www.crystaldecisions.com
Support services:
http://support.crystaldecisions.com