既存のセキュリティ設定をプログラムで変更する方法
こちらの記事では、既存のユーザ設定をプログラムで変更する方法をご紹介します。
ユーザロールを追加/削除したい、有効期限設定を変更したい、等の場合にお役立てください。
なお、ユーザ設定をプログラムで新規作成する方法は こちら の記事で紹介しております。
// ユーザを指定してロールを確認する(以下の例は、test ユーザ)
USER>zn"%SYS"%SYS>do##class(Security.Users).Get("test",.prop)
; 全プロパティ確認 %SYS>zwrite prop
prop("AccountNeverExpires")=0
prop("AutheEnabled")=0
prop("ChangePassword")=0
:
prop("Roles")="%DB_USER,%Developer,testrole"; もしくは以下で直接取得$SYS>write prop("Roles")
%DB_USER,%Developer,testrole
; ロールの追加をしたいとき%SYS>write##class(Security.Users).AddRoles("test","%SQL")
1%SYS>kill%SYS>do##class(Security.Users).Get("test",.prop)
%SYS>write prop("Roles")
%DB_USER,%Developer,%SQL,testrole
; Modify() メソッドでロールを変更することも可能%SYS>set prop("Roles")="%Developer,testrole"%SYS>write##class(Security.Users).Modify("test",.prop)
1%SYS>kill%SYS>do##class(Security.Users).Get("test",.prop)
%SYS>w prop("Roles")
%Developer,testrole
; ロールの削除をしたいとき(複数の時はカンマ区切り)%SYS>write##class(Security.Users).RemoveRoles("test","testrole")
1%SYS>kill%SYS>do##class(Security.Users).Get("test",.prop)
%SYS>write prop("Roles")
%Developer; 指定されたロール (またはロールのリスト) に特定のSQL特権を付与する場合; 以下の例は、testroleロールに スキーマ=Sampleの、Insert,Update,Select,Delete権限を付与する; 権限を付与したいスキーマ・テーブルのあるネームスペースで実行する%SYS>zn"user"
USER>write$SYSTEM.SQL.Security.GrantPrivilege("Insert,Update,Select,Delete","Sample","Schema","testrole")
1
%SYS>set stmt=##class(%SQL.Statement).%New()
%SYS>write stmt.%PrepareClassQuery("Security.Users","List")
1%SYS>set rs=stmt.%Execute()
%SYS>while rs.%Next() { write !,rs.%Get("Name")," || ",rs.%Get("Roles") }
Admin || %EnsRole_Administrator,%EnsRole_Developer,%Manager
CSPSystem ||
IAM || %IAM_API
SuperUser || %All
UnknownUser || %All
_Ensemble || %All
_PUBLIC ||
_SYSTEM || %All
test ||
※Listクエリの詳細情報はこちら、さらに詳しい情報が必要な場合は Detailクエリ を使用してください。
Webアプリケーション情報(Security.Applications)や、ロールの情報(Security.Roles)等の、セキュリティ情報についても、同様の手順で参照・変更することが可能です。
*「RESTのディスパッチクラス」を参照・変更したいとき
// 既存のWebアプリケーション設定のディスパッチクラスを編集する場合%SYS>do##class(Security.Applications).Get("/csp/user/rest",.prop)
%SYS>zwrite prop
prop("AutheEnabled")=64
prop("AutoCompile")=1
prop("CSPZENEnabled")=1
prop("CSRFToken")=0
prop("ChangePasswordPage")=""
prop("CookiePath")="/csp/user/rest/"
prop("DeepSeeEnabled")=0
prop("Description")=""
prop("DispatchClass")="User.REST"
:
%SYS>set Properties("DispatchClass")="User.REST2"// ディスパッチクラスを変更%SYS>write##class(Security.Applications).Modify("/csp/user/rest",.Properties)
1
*「許可された認証方法」を参照・変更したいとき(例:認証なし+パスワード ⇒ パスワード)
%SYS>do##class(Security.Applications).Get("/csp/user",.prop)
%SYS>zwrite prop
prop("AutheEnabled")=96// 96 : 0110 0000(Bit 5 + Bit 6)
prop("AutoCompile")=1
prop("CSPZENEnabled")=1
:
%SYS>set prop("AutheEnabled")=32// 32 : 0010 0000 (Bit 5)%SYS>write##class(Security.Applications).Modify("/csp/user",.prop)
1// AutheEnabled// Bit 2 = AutheK5API// Bit 4 = AutheOS// Bit 5 - AuthePassword// Bit 6 = AutheUnauthenticated// Bit 11 = AutheLDAP// Bit 13 = AutheDelegated// Bit 14 = LoginToken// Bit 20 = TwoFactorSMS// Bit 21 = TwoFactorPW
詳細はクラスリファレンスをご覧ください。
ディスカッション (0)0