33 |
|
|
34 |
class MySQLPwDB(simple.SimplePwDB): |
class MySQLPwDB(simple.SimplePwDB): |
35 |
def __init__(self): |
def __init__(self): |
36 |
|
self.db = db |
37 |
q_sites = """ |
q_sites = """ |
38 |
select id, name, ip_address, port, location |
select id, name, ip_address, port, location |
39 |
from site order by name |
from site order by name |
57 |
port=port, |
port=port, |
58 |
location=location, |
location=location, |
59 |
user_list=user_list)) |
user_list=user_list)) |
60 |
|
#print 'site ' + name + ' grants access to ' + user_list.__repr__() |
61 |
|
|
62 |
simple.SimplePwDB.__init__(self, site_list) |
simple.SimplePwDB.__init__(self, site_list) |
|
db.close() |
|
|
|
|
|
|
|
63 |
|
|
64 |
|
#def __del__(self): |
65 |
|
# db.close() |
66 |
|
|
67 |
|
def get_site(self, sid): |
68 |
|
user = None |
69 |
|
if sid.find('@') >= 0: |
70 |
|
user, sid = sid.split('@') |
71 |
|
if not user: |
72 |
|
q_user = """ |
73 |
|
select uid from site,user where site.id = user.site_id and site.name = '%s' |
74 |
|
order by `primary` desc limit 1 |
75 |
|
""" |
76 |
|
users = db.cursor() |
77 |
|
users.execute(q_user % sid) |
78 |
|
user = users.fetchone() |
79 |
|
if not user or not len(user): |
80 |
|
return None, None |
81 |
|
user = user[0] |
82 |
|
users.close() |
83 |
|
if not self.can_connect(user, sid): |
84 |
|
print 'User \'%s\' is not allowed to connect to \'%s\'' % (user, sid) |
85 |
|
return None, None |
86 |
|
return self.sites[sid].default_user(), self.sites[sid] |
87 |
|
|
88 |
|
def is_allowed(self, user, passwd): |
89 |
|
q_access = """ |
90 |
|
select count(*) from login where uid = '%s' and `password` = '%s'""" |
91 |
|
logins = db.cursor() |
92 |
|
logins.execute(q_access % (user, passwd)) |
93 |
|
login = logins.fetchone()[0] |
94 |
|
logins.close() |
95 |
|
if login: |
96 |
|
self.login = user |
97 |
|
return login |
98 |
|
|
99 |
|
def can_connect(self, user, site): |
100 |
|
q_group = """ |
101 |
|
select count(*) |
102 |
|
from login,login_profile,profile,profile_sgroup,sgroup,sgroup_site,site,user |
103 |
|
where login.uid = '%s' |
104 |
|
and login.id = login_profile.login_id |
105 |
|
and login_profile.profile_id = profile.id |
106 |
|
and profile.id = profile_sgroup.profile_id |
107 |
|
and profile_sgroup.sgroup_id = sgroup.id |
108 |
|
and sgroup.id = sgroup_site.sgroup_id |
109 |
|
and sgroup_site.site_id = site.id |
110 |
|
and site.name = '%s' |
111 |
|
and user.site_id = site.id |
112 |
|
and user.uid = '%s' |
113 |
|
""" |
114 |
|
group = db.cursor() |
115 |
|
group.execute(q_group % (self.login, site, user)) |
116 |
|
gr = group.fetchone()[0] |
117 |
|
print 'requete can_connect: %d' % gr |
118 |
|
group.close() |
119 |
|
return gr |