DEV Community

InterSystems Developer for InterSystems

Posted on • Originally published at community.intersystems.com

Add a Web application by code

Hi community,

I'm going to publish several articles on how to perform actions that you can do in the web portal but via code.

Today.... Web Applications via code<!--break-->

Introduction 

If you want to add a new Web application, you you usually do it with the option System Administration - Security - Applications - Web Applications

Image description

Image description

Then add the roles to use in the application

Image description

 

But, if you have not access to the management portal, you can do it by code.

RegisterWebApplication

With this method, a new web application can be registered via code.

/// Register a web application. It configure only services that doesn't need special access.
///
    ///
  • pNameSpace Name of the namespace when the web application runs.
  • ///
  • pName Service name to create/update.
  • ///
  • pClassName Name of class to run.
  • ///
  • pDescription Comment to include.
  • ///

/// &lt;example>
/// // Add web api
/// Do myClass.RegisterWebApplication("SAMPLES","/myApp/api","Host.RS.Rest","This is my api rest")&lt;/example> ClassMethod RegisterWebApplication(pNameSpace As %String, pName As %String, pClassName As %String, pDescription As %String = "") As %Status
{
    New $Namespace
    Set $Namespace = "%SYS"
    Set ret = $$$OK
    Set spec("AutheEnabled") = $$$AutheUnauthenticated
    Set spec("NameSpace") = pNameSpace
    Set spec("Description") = pDescription
    Set spec("IsNameSpaceDefault") = $$$NO
    Set spec("DispatchClass") = pClassName
    Set spec("MatchRoles")=":%All"

    If ('##class(Security.Applications).Exists(pName)) {
        Write !,"Creating Web application """pName"""..."
        Set ret = ##class(Security.Applications).Create(pName, .spec)
        Write !, "Web application """pName""" is created."
    }
    Else { // ensure configuration matches in case of updating from old terminal versions
        Write !, "Updating web application """pName"""..."
        Set ret = ##class(Security.Applications).Modify(pName, .spec)
        Write !, "Web application """pName""" is updated."
    }
    Return ret
}

By default, it uses the %All roles, but you can modify according your needs

 

RemoveWebApplication

/// Remove a web application
///
    ///
  • pName Name of web application to remove.
  • ///
  • pClassName Name of the class linked to the web application.
  • ///

/// &lt;example>
/// // Remove web api
/// Do myClass.RemoveWebApplication("/myApp/api","Host.RS.Rest")&lt;/example>
ClassMethod RemoveWebApplication(pName As %String, pClassName As %String)
{
    New $Namespace
    Set $Namespace = "%SYS"
    Set ret = $$$OK
    If (##class(Security.Applications).Exists(pName)) {
        Do ##class(Security.Applications).Get(pName, .props)
        If (props("DispatchClass") '= pClassName) {
            Write !, "Web application doesn't refer to DispatchClass "pClassName
        }
        Else {
            Write !, "Deleting Web application """_pName
"""..."
            Set ret = ##class(Security.Applications).Delete(pName)
            Write !, "Web application """pName""" was successfully deleted."
        }
    }
    Return ret
}

For security, the name of the class is necesary to check that you are not removing a web application by error.

I hope it helps you.

Best regards,

Kurro Lopez

 

Top comments (0)