Unable to add user to a group

You can share your Lua Scripts with everybody here.

Unable to add user to a group

Postby dwatson » Wed Nov 30, 2011 1:20 am

Hi,

We need to add a bunch of users to a group. We have already created the group in Admin console. We created a script to add the users to the group but that doesn't seem to work - after running the script, we do not see the group in the group tab for the user account. Can anyone suggest what we are doing wrong in the following snippet?

Code: Select all
local domain = "mydomain"   
local username = "user1"
local group = {}
table.insert(group, "testgroup")

local myuser = c_GetUser(domain,username)

if myuser ~= nil then
    myuser.oldpassword = myuser.password

    for k,v in pairs(myuser) do
        if type(v) == "boolean" then
            if v == true then
                myuser[k] = 1
            else
                myuser[k] = 0
            end
        end
    end
   
    table.insert(myuser.usergroups, group)

    AddUser(domain,myuser) 
end



Thanks
Derek
dwatson
 
posts 2
 
joined Wed Nov 30, 2011 1:12 am

Re: Unable to add user to a group

Postby FTP » Wed Nov 30, 2011 9:12 am

OK, please use the following Lua scripts:

Code: Select all
local domain = "mydomain"   
local username = "user1"
local group = {}
group.groupname = "testgroup"

local myuser = c_GetUser(domain,username)

if myuser ~= nil then
    myuser.oldpassword = myuser.password

    for k,v in pairs(myuser) do
        if type(v) == "boolean" then
            if v == true then
                myuser[k] = 1
            else
                myuser[k] = 0
            end
        end
    end
   
    if myuser.usergroups ~= nil then
   local bGroupExisted = false
   for _,tempgroup in pairs(myuser.usergroups) do
      if tempgroup.groupname == group.groupname then
         bGroupExisted = true
      end
   end
   if bGroupExisted == false then
      table.insert(myuser.usergroups, group)
   end
    else
   myuser.usergroups = {}
   table.insert(myuser.usergroups, group)
    end

    AddUser(domain,myuser) 
end
FTP
Site Admin
 
posts 1232
 
joined Tue Sep 29, 2009 6:09 am

Re: Unable to add user to a group

Postby dwatson » Wed Nov 30, 2011 6:52 pm

That worked like a charm.

And, thanks a lot for guiding a novice lua user the proper way of accessing and manipulating the necessary objects. I really appreciate it.

Derek
dwatson
 
posts 2
 
joined Wed Nov 30, 2011 1:12 am


Return to Lua scripts

Who is online

Users browsing this forum: No registered users and 0 guests