0
0

Pleasanter Tips: データの編集権限を付与する

Last updated at Posted at 2023-11-29

機能説明

Pleasanterでデータの編集権限を付与できるようにしたいという要望があったので作成しました。
データ編集できるのは、基本的に作成者、またはテナント管理者のみになります。
下部のアクセス付与で指定したユーザーやグループのメンバーはデータ編集可能です。但しアクセス付与はできません。
それ以外のユーザーはデータの閲覧はできますが、データ編集はできません。但し、コメントの入力は可能です。
編集可能の可否については下部にメッセージを表示するようにしました。
2023/11/20:組織選択も追加しました。

画面サンプル

image.png

項目

サンプルでは下記の項目を追加しました。

項目 タイトル 設定
分類B ユーザーアクセス [[Users]]複数選択可能
分類C グループアクセス [[Groups]]複数選択可能
分類D 組織アクセス [[Depts]]複数選択可能

コード(サーバスクリプト:画面表示の前)

//画面表示の前
if( context.Action == 'new' || context.Action == 'edit' ) {
  //アイテムデータ取得
  let res = items.Get(context.Id);
  if( res.Length == 1 ){
    EditAuth( res );
  }  
}
function EditAuth( res ) {
  context.Log( `UserId:${context.UserId}` );
  context.Log( `Creator:${res[0].Creator}` );
  context.Log( `Users:${res[0].ClassB}` );
  context.Log( `Groups:${res[0].ClassC}` );
  context.Log( `Depts:${res[0].ClassD}` );
  //ユーザー情報
  let user = users.Get( context.UserId );
  context.Log( `TenantManager:${user.TenantManager}` );
  //アクセス権限チェック
  let mode = -1;
  if( res[0].Creator == context.UserId || user.TenantManager == true ) {
    //作成者または管理者だった場合
    mode = 0;
  } else {
    //作成者以外の場合の編集可能ユーザー
    if( res[0].ClassB !== "" ) {
      let userIdList = JSON.parse( res[0].ClassB );
      for(let userId of userIdList) {
        context.Log( userId );
        if( userId == context.UserId ) {
          mode = 1;
          break;
        }
      }
    }
    //作成者以外の場合の編集可能グループ
    if( res[0].ClassC !== "" ) {
      let groupIdList = JSON.parse( res[0].ClassC );
      for(let groupId of groupIdList) {
        context.Log( groupId );
        let group = groups.Get( groupId );
        if (group) {
          context.Log( `Group:[${group.GroupId}]${group.GroupName}` );
          if( group.ContainsUser( context.UserId ) ) {
            mode = 1;
            break;
          }
        }
      }
    }
    //作成者以外の場合の編集可能組織
    if( res[0].ClassD !== "" ) {
      let deptIdList = JSON.parse( res[0].ClassD );
      for( let deptId of deptIdList ) {
        context.Log( deptId );
        let dept = depts.Get( deptId );
        if( dept ) {
          context.Log( `Dept:[${dept.DeptId}]${dept.DeptName}` );
          const members = dept.GetMembers();
          for (let member of members) {
            context.Log(`User:[${member.UserId}]${member.Name}`);
            if( member.UserId == context.UserId ){
              mode = 1;
              break;
            }
          }
        }
      }
    }
  }
  context.Log( `Mode:${mode}` );
  if( mode == 0 ) {
    //作成者
  } else if( mode == 1 ) {
    //権限有り
    columns.ClassB.ReadOnly = true;  
    columns.ClassC.ReadOnly = true;  
    columns.ClassD.ReadOnly = true;  
    context.AddMessage( '編集権限が有ります。', 'alert-warning' );
  } else {
    //権限無し
    columns.Title.ReadOnly = true;
    columns.Body.ReadOnly = true;
    columns.Status.ReadOnly = true;
    columns.Owner.ReadOnly = true;
    columns.Manager.ReadOnly = true;
    columns.ClassA.ReadOnly = true;
    columns.ClassB.ReadOnly = true;  
    columns.ClassC.ReadOnly = true;
    columns.ClassD.ReadOnly = true;
    context.AddMessage( '編集権限が有りません。コメントのみ記入できます。', 'alert-warning' );
  }
}

最後に

この機能は作成者が自由に編集権限を付与できるという大変便利な機能になると思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0