Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Restrict a remote Snif method by role

Give two authenticated users access to the same draft review service. Both can read a draft's stage. Only the approver can change it.

Declare the class and method roles

uses
  System.SysUtils, StreamSec.Snif.ClassServer, StreamSec.Snif.ServerAttributes,
  StreamSec.Snic.ClassClient, TaskSnifCall;
type
  {$M+}
  IDraftReview = interface
    ['{73920B19-1FA6-411C-B751-4E0B2825EC68}']
    function ReadStage: string;
    function Approve: string;
  end;
  {$M-}

  {$TYPEINFO ON}
  {$METHODINFO ON}
  [InstanceKind(perConnection), Roles('review')]
  TDraftReview = class
  private
    fApproved: Boolean;
  public
    function ReadStage: string;
    [Roles('approve')]
    function Approve: string;
  end;

Put Roles('review') on the server class and Roles('approve') on its Approve method. An ordinary call to ReadStage requires the class role. Calling Approve requires both roles. The Delphi interface gives the two clients the same methods to call. The server checks permissions on each call.

function TDraftReview.ReadStage: string;
begin
  if fApproved then Result := 'approved'
  else Result := 'pending';
end;

function TDraftReview.Approve: string;
begin
  fApproved := True;
  Result := ReadStage;
end;

perConnection gives each connection its own draft in this example. Reading its stage after a call shows whether the method changed it.

Register the service and assign the roles

procedure ConfigureReviews(aServer: TsnifClassServer;
  const aReaderPassword, aApproverPassword: string);
var
  lUsers: TsnifUserList;
begin
  if TsnifClassServer.FindServerClass(TDraftReview.QualifiedClassName) = nil then
    TsnifClassServer.RegisterServerClass(TDraftReview);

  SetLength(lUsers, 2);
  lUsers[0] := NewSnifUser('reader', aReaderPassword, ['review']);
  lUsers[1] := NewSnifUser('approver', aApproverPassword, ['review', 'approve']);
  aServer.UpdateUserList(lUsers);
end;

Run ConfigureReviews on a new server before starting its listener. UpdateUserList loads the entire account list. In an existing deployment, include these role assignments in the account data you already load. Supply each user's password when provisioning the example. The NewSnifUser helper creates the KAPA registration record as shown in Expose a Delphi class through Snif and call it from a client.

Call the same method as each user

function OpenReview(aClient: TsnicClassClient): IDraftReview;
begin
  if not aClient.MakeInstance('TaskSnifRoles.TDraftReview', TypeInfo(IDraftReview),
    Result, '') then
    raise Exception.Create('Cannot create the draft review proxy');
end;

Use a separate authenticated client connection for each account and call OpenReview on each one. The empty role argument to MakeInstance leaves Snif to check the authenticated user's roles on the server. Creating the proxy is local. The method call sends the request.

The Snif remoting concepts describe class roles, method roles and instance lifetimes.

Reference

TsnifClassServer · TsnicClassClient · StreamSec.Snif.ServerAttributes · StreamSec.Snif.ClassServer · StreamSec.Snic.ClassClient · StreamSec.Snif.Response

Related tasks