1 |
#!/usr/bin/env python
|
2 |
from optparse import OptionParser
|
3 |
from Models import Location
|
4 |
from Tools.SecurityModuleCore.SecurityDBApi import SecurityDBApi
|
5 |
from Framework import Context
|
6 |
from Framework.Logger import Logger
|
7 |
import sys, re
|
8 |
import traceback
|
9 |
import os
|
10 |
|
11 |
def createSecurityDBApi (context):
|
12 |
webtoolsRoot = os.getenv ("WEBTOOLS_ROOT")
|
13 |
if not os.getenv ("WEBTOOLS_ROOT"):
|
14 |
print "ERROR: WEBTOOLS_ROOT must be defined."
|
15 |
os.environ["SEC_MOD_INI"] = os.path.join (webtoolsRoot,
|
16 |
"lib/python2.4/site-packages/Tools/SiteDBCore/security.ini")
|
17 |
assert (os.path.exists (os.environ["SEC_MOD_INI"]))
|
18 |
return SecurityDBApi (context)
|
19 |
|
20 |
def checkResults (obj):
|
21 |
if type (obj) != dict:
|
22 |
return False
|
23 |
if not obj.has_key (0):
|
24 |
return False
|
25 |
return True
|
26 |
|
27 |
class Application (object):
|
28 |
def __init__ (self):
|
29 |
self.__parser = OptionParser ()
|
30 |
self.__parser.add_option ("-g", "--group-id",
|
31 |
dest="groupName",
|
32 |
default=None)
|
33 |
self.__parser.add_option ("-u", "--user",
|
34 |
dest="user",
|
35 |
default="")
|
36 |
|
37 |
self.__context = Context ()
|
38 |
logger = Logger ("Main")
|
39 |
self.__context.addService (logger)
|
40 |
self.__securityApi = createSecurityDBApi (self.__context)
|
41 |
|
42 |
|
43 |
def run (self):
|
44 |
result = self.__parser.parse_args ()
|
45 |
opts, args = result
|
46 |
if not opts.groupName:
|
47 |
self.__parser.error ("-g/--group-id option not specified.")
|
48 |
|
49 |
if not opts.user:
|
50 |
self.__parser.error ("-u/--user option not specified.")
|
51 |
|
52 |
|
53 |
locations = Location.select (Location.q.name == opts.groupName)
|
54 |
if not locations.count ():
|
55 |
print "ERROR: could not find group %s." % opts.groupName
|
56 |
sys.exit (1)
|
57 |
|
58 |
groupId = locations[0].id
|
59 |
users = self.__securityApi.api.getDataObject (["id"],
|
60 |
"select id from contact where username = :username",
|
61 |
{"username": opts.user})
|
62 |
if not checkResults (users):
|
63 |
print "ERROR: could not find user %s." % opts.user
|
64 |
sys.exit (1)
|
65 |
|
66 |
userId = users[0]["id"]
|
67 |
|
68 |
roles = self.__securityApi.api.getDataObject (["id"],
|
69 |
"select id from role where title = :title",
|
70 |
{"title": "Production Operator"})
|
71 |
|
72 |
if not checkResults (users):
|
73 |
print "ERROR: could not find role 'Production Operator'."
|
74 |
sys.exit (1)
|
75 |
|
76 |
roleId = roles[0]["id"]
|
77 |
#print userId, groupId, roleId
|
78 |
self.__securityApi.grantUserGroupRole (userId, groupId, roleId)
|
79 |
|
80 |
if __name__ == "__main__":
|
81 |
app = Application ()
|
82 |
app.run ()
|
83 |
|