Author: mimir
Date: 2007-01-18 21:50:24 +0000 (Thu, 18 Jan 2007)
New Revision: 20881

WebSVN: 
http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=20881

Log:
Sorting out NetUsrCtx methods. Still a couple of things left.


rafal


Modified:
   branches/SAMBA_4_0/source/scripting/ejs/ejsnet/net_user.c


Changeset:
Modified: branches/SAMBA_4_0/source/scripting/ejs/ejsnet/net_user.c
===================================================================
--- branches/SAMBA_4_0/source/scripting/ejs/ejsnet/net_user.c   2007-01-18 
19:18:43 UTC (rev 20880)
+++ branches/SAMBA_4_0/source/scripting/ejs/ejsnet/net_user.c   2007-01-18 
21:50:24 UTC (rev 20881)
@@ -37,7 +37,7 @@
 
 /*
   Usage:
-  usrCtx = net.UserMgr(domain);
+  usrCtx = net.UserMgr(domain = <default from credentials>);
 */
 int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv)
 {
@@ -46,68 +46,102 @@
        const char *userman_domain = NULL;
        struct MprVar *obj = NULL;
 
-       ctx = mprGetThisPtr(eid, "ctx");
-       mem_ctx = talloc_new(mprMemCtx());
-
+       /* fetch the arguments: domain name */
        if (argc == 0) {
+               /* default domain name is supplied in credentials */
                userman_domain = cli_credentials_get_domain(ctx->cred);
 
        } else if (argc == 1 && mprVarIsString(argv[0]->type)) {
+               /* domain name can also be specified explicitly 
+                  (e.g. to connect remote domain) */
                userman_domain = talloc_strdup(ctx, mprToString(argv[0]));
 
        } else {
                ejsSetErrorMsg(eid, "too many arguments");
                goto done;
        }
+
+       /* libnet context */
+       ctx = mprGetThisPtr(eid, "ctx");
+       if (ctx == NULL) {
+               ejsSetErrorMsg(eid, "ctx property returns null pointer");
+               goto done;
+       }
        
-       if (!userman_domain) {
+       mem_ctx = talloc_new(mprMemCtx());
+
+       /* any domain name must be specified anyway */
+       if (userman_domain == NULL) {
                ejsSetErrorMsg(eid, "a domain must be specified for user 
management");
                goto done;
        }
+       
+       /* create 'net user' subcontext */
+       obj = mprInitObject(eid, "NetUsrCtx", argc, argv);
 
-       obj = mprInitObject(eid, "NetUsrCtx", argc, argv);
+       /* add properties */
        mprSetPtrChild(obj, "ctx", ctx);
        mprSetPtrChild(obj, "domain", userman_domain);
 
+       /* add methods */
        mprSetStringCFunction(obj, "Create", ejs_net_createuser);
        mprSetStringCFunction(obj, "Delete", ejs_net_deleteuser);
        mprSetStringCFunction(obj, "Info", ejs_net_userinfo);
        mprSetCFunction(obj, "List", ejs_net_userlist);
 
-       return 0;
 done:
        talloc_free(mem_ctx);
-       return -1;
+       return 0;
 }
 
 
+/*
+  Usage:
+  NTSTATUS = NetUsrCtx.Create(Username)
+*/
 static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv)
 {
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
        TALLOC_CTX *mem_ctx;
        struct libnet_context *ctx;
        const char *userman_domain = NULL;
+       const char *username = NULL;
        struct libnet_CreateUser req;
 
-       if (argc != 1) {
-               ejsSetErrorMsg(eid, "argument 1 must be a string");
-               return -1;
+       mem_ctx = talloc_new(mprMemCtx());
+       if (mem_ctx == NULL) {
+               ejsSetErrorMsg(eid, "could not create memory context - out of 
memory");
+               goto done;
        }
 
+       /* fetch the arguments: username */
+       if (argc == 0) {
+               ejsSetErrorMsg(eid, "too little arguments");
+               goto done;
+
+       } else if (argc == 1) {
+               username = argv[0];
+
+       } else {
+               ejsSetErrorMsg(eid, "too many arguments");
+               goto done;
+       }
+       
+       /* libnet context */
        ctx = mprGetThisPtr(eid, "ctx");
-       if (!ctx) {
+       if (ctx == NULL) {
                ejsSetErrorMsg(eid, "ctx property returns null pointer");
-               return -1;
+               goto done;
        }
 
+       /* domain where the account is to be created */
        userman_domain = mprGetThisPtr(eid, "domain");
-       if (!userman_domain) {
+       if (userman_domain == NULL) {
                ejsSetErrorMsg(eid, "domain property returns null pointer");
-               return -1;
+               goto done;
        }
        
-       mem_ctx = talloc_new(mprMemCtx());
-
+       /* call the libnet function */
        req.in.domain_name = userman_domain;
        req.in.user_name   = argv[0];
 
@@ -116,83 +150,125 @@
                ejsSetErrorMsg(eid, "%s", req.out.error_string);
        }
 
+done:
        talloc_free(mem_ctx);
        mpr_Return(eid, mprNTSTATUS(status));
        return 0;
 }
 
 
+/*
+  Usage:
+  NTSTATUS = NetUsrCtx.Delete(Username)
+*/
 static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv)
 {
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
        TALLOC_CTX *mem_ctx;
        struct libnet_context *ctx;
        const char *userman_domain = NULL;
+       const char *username = NULL;
        struct libnet_DeleteUser req;
 
-       if (argc != 1) {
-               ejsSetErrorMsg(eid, "argument 1 must be a string");
-               return -1;
+       mem_ctx = talloc_new(mprMemCtx());
+       if (mem_ctx == NULL) {
+               ejsSetErrorMsg(eid, "could not create memory context - out of 
memory");
+               goto done;
        }
 
+       /* fetch the arguments: username */
+       if (argc == 0) {
+               ejsSetErrorMsg(eid, "too little arguments");
+               goto done;
+
+       } else if (argc == 1) {
+               username = argv[0];
+
+       } else {
+               ejsSetErrorMsg(eid, "too many arguments");
+               goto done;
+       }
+
+       /* libnet context */
        ctx = mprGetThisPtr(eid, "ctx");
-       if (!ctx) {
+       if (ctx == NULL) {
                ejsSetErrorMsg(eid, "ctx property returns null pointer");
-               return -1;
+               goto done;
        }
-
+       
+       /* domain where the account is to be deleted */
        userman_domain = mprGetThisPtr(eid, "domain");
        if (!userman_domain) {
                ejsSetErrorMsg(eid, "domain property returns null pointer");
-               return -1;
+               goto done;
        }
        
-       mem_ctx = talloc_new(mprMemCtx());
-
+       /* call the libnet function */
        req.in.domain_name = userman_domain;
-       req.in.user_name   = argv[0];
+       req.in.user_name   = username;
 
        status = libnet_DeleteUser(ctx, mem_ctx, &req);
        if (!NT_STATUS_IS_OK(status)) {
                ejsSetErrorMsg(eid, "%s", req.out.error_string);
        }
 
+done:
        talloc_free(mem_ctx);
        mpr_Return(eid, mprNTSTATUS(status));
        return 0;
 }
 
 
+/*
+  Usage:
+  UserInfo = NetUsrCtx.Info(Username)
+*/
 static int ejs_net_userinfo(MprVarHandle eid, int argc, char **argv)
 {
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
        TALLOC_CTX *mem_ctx;
        struct libnet_context *ctx;
-       const char *userman_domain;
+       const char *userman_domain = NULL;
+       const char *username = NULL;
        struct libnet_UserInfo req;
        struct MprVar mprUserInfo;
 
-       if (argc != 1) {
-               ejsSetErrorMsg(eid, "argument 1 must be a string");
-               return -1;
+       mem_ctx = talloc_new(mprMemCtx());
+       if (mem_ctx == NULL) {
+               ejsSetErrorMsg(eid, "could not create memory context - out of 
memory");
+               goto done;
        }
+       
+       /* fetch the arguments: username */
+       if (argc == 0) {
+               ejsSetErrorMsg(eid, "too little arguments");
+               goto done;
 
+       } else if (argc == 1) {
+               username = argv[0];
+
+       } else {
+               ejsSetErrorMsg(eid, "too many arguments");
+               goto done;
+       }
+
+       /* libnet context */
        ctx = mprGetThisPtr(eid, "ctx");
        if (ctx == NULL) {
                ejsSetErrorMsg(eid, "ctx property returns null pointer");
-               return -1;
+               goto done;
        }
-
+       
+       /* domain where the user account is to be queried */
        userman_domain = mprGetThisPtr(eid, "domain");
        if (userman_domain == NULL) {
                ejsSetErrorMsg(eid, "domain property returns null pointer");
                return -1;
        }
 
-       mem_ctx = talloc_new(mprMemCtx());
-       
+       /* call the libnet function */
        req.in.domain_name = userman_domain;
-       req.in.user_name   = argv[0];
+       req.in.user_name   = username;
        
        status = libnet_UserInfo(ctx, mem_ctx, &req);
        if (!NT_STATUS_IS_OK(status)) {
@@ -213,19 +289,30 @@
 }
 
 
+/*
+  Usage:
+  UserListCtx = NetUsrCtx.List(UserListCtx)
+*/
 static int ejs_net_userlist(MprVarHandle eid, int argc, struct MprVar **argv)
 {
        TALLOC_CTX *mem_ctx;
        NTSTATUS status;
        struct libnet_context *ctx;
        const char *userlist_domain;
+       int page_size = 10;         /* TODO: this should be specified in a 
nicer way */
        struct libnet_UserList req;
        struct MprVar mprListCtx, *mprInListCtx;
        
        mem_ctx = talloc_new(mprMemCtx());
+       if (mem_ctx == NULL) {
+               ejsSetErrorMsg(eid, "could not create memory context - out of 
memory");
+               goto done;
+       }
        
+       /* fetch the arguments */
        if (argc == 0) {
                ejsSetErrorMsg(eid, "too little arguments");
+               goto done;
 
        } else if (argc == 1) {
                if (mprVarIsObject(argv[0]->type)) {
@@ -234,6 +321,7 @@
                        req.in.resume_index = 
mprListGetResumeIndex(mprInListCtx);
 
                } else {
+                       /* this is a first call */
                        req.in.resume_index = 0;
                }
 
@@ -242,20 +330,23 @@
                goto done;
        }
 
+       /* libnet context */
        ctx = mprGetThisPtr(eid, "ctx");
-       if (!ctx) {
+       if (ctx == NULL) {
                ejsSetErrorMsg(eid, "ctx property returns null pointer");
-               return -1;
+               goto done;
        }
-
+       
+       /* domain where user accounts are to be enumerated */
        userlist_domain = mprGetThisPtr(eid, "domain");
-       if (!userlist_domain) {
+       if (userlist_domain == NULL) {
                ejsSetErrorMsg(eid, "domain property returns null pointer");
-               return -1;
+               goto done;
        }
 
+       /* call the libnet function */
        req.in.domain_name   = userlist_domain;
-       req.in.page_size     = 10;  /* TODO: this should be specified in a 
nicer way */
+       req.in.page_size     = page_size;
        
        status = libnet_UserList(ctx, mem_ctx, &req);
        if (!NT_STATUS_IS_OK(status) &&

Reply via email to