Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added new users backend example

git-svn-id: https://camelo.conectiva/repos/projetos/qtlab/mcc2@8944 aeaf0dc8-91d7-0310-8701-e1fc1a66ab8d
  • Loading branch information...
commit 2bcd3c5f3f306bef743b7f0324fb54881681d819 1 parent 113bf24
wiliam authored
View
19 examples/users/group-details.py
@@ -4,5 +4,20 @@
'org.mandrivalinux.mcc2.Users',
'/org/mandrivalinux/mcc2/Users')
interface = dbus.Interface(proxy, 'org.mandrivalinux.mcc2.Users')
-result = interface.GroupDetails('john')
-print result
+result = interface.GroupDetails('paula')
+print result
+#print type(result)
+#d = dict(result)
+#print type(d)
+#print d
+#print dir(result)
+new_dict = {}
+for key, value in result.items():
+ if key == 'members':
+ new_list = []
+ for list_value in value:
+ new_list.append(str(list_value))
+ new_dict[str(key)] = new_list
+ break
+ new_dict[str(key)] = str(value)
+print new_dict
View
35 examples/users/modify-user.py
@@ -5,6 +5,35 @@
'/org/mandrivalinux/mcc2/Users')
interface = dbus.Interface(proxy, 'org.mandrivalinux.mcc2.Users')
+
+import time
+import math
+
+date = '2011-04-06'
+
+year = None
+month = None
+day = None
+
+(year, month, day) = date.split('-')
+
+try:
+ tmp = time.mktime([int(year), int(month), int(day), 0, 0, 0, 0, 0, -1])
+except OverflowError:
+ print 'The year is out of range. Please select a different year'
+
+seconds = 24 * 60 * 60
+days_expire = tmp / seconds
+fraction, integer = math.modf(days_expire)
+
+if fraction == 0.0:
+ days_expire = integer
+else:
+ days_expire = integer + 1
+
+
+print int(days_expire)
+
user_info = {
'username': 'john',
'new_username': 'johns',
@@ -14,7 +43,7 @@
'gid': 666,
'home_directory': '/home/john',
'password': 'secret2',
- #'shadow_expire': 'YYYY-MM-DD',
+ #'shadow_expire': days_expire,
#'shadow_min': 0,
#'shadow_max', 99999,
#'shadow_warning': 7,
@@ -22,5 +51,5 @@
#'shadow_last_change': 'YYYY-MM-DD'
}
-result = interface.ModifyUser(user_info)
-print result
+#result = interface.ModifyUser(user_info)
+#print result
View
31 examples/users/users-details.py
@@ -0,0 +1,31 @@
+import time
+
+import dbus
+bus = dbus.SystemBus()
+proxy = bus.get_object(
+ 'org.mandrivalinux.mcc2.Users',
+ '/org/mandrivalinux/mcc2/Users')
+interface = dbus.Interface(proxy, 'org.mandrivalinux.mcc2.Users')
+result = interface.UserDetails('bedi')
+print result['uid']
+print result['gid']
+print result['userName']
+print result['fullName']
+print result['homeDirectory']
+print result['loginShell']
+
+print result['shadowExpire']
+
+if result['shadowExpire']:
+ days = int(result['shadowExpire'])
+ tmp = days * int(24 * 60 * 60)
+ age = time.localtime(tmp)
+ print time.strftime('%Y %m %d', age)
+else:
+ print result['shadowExpire']
+
+print result['shadowMin']
+print result['shadowMax']
+print result['shadowWarning']
+print result['shadowInactive']
+print result['shadowLastChange']
View
8 mcc2/backends/services/service.py
@@ -4,7 +4,7 @@
import dbus.service
import dbus.mainloop.glib
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
-from mcc2.backends.policykit import check_authorization
+from mcc2.backends.policykit import checkAuthorization
__all__ = ['Services']
@@ -78,7 +78,7 @@ def Start(self, name, mode, sender, connection):
if not self.is_systemd_running:
raise dbus.DBusException, self.msg['not_running']
- check_authorization(sender, connection, self.__action)
+ checkAuthorization(sender, connection, self.__action)
return self.__systemd_interface.StartUnit(name, mode)
@@ -101,7 +101,7 @@ def Stop(self, name, mode, sender, connection):
if not self.is_systemd_running:
raise dbus.DBusException, self.msg['not_running']
- check_authorization(sender, connection, self.__action)
+ checkAuthorization(sender, connection, self.__action)
return self.__systemd_interface.StopUnit(name, mode)
@@ -124,7 +124,7 @@ def Restart(self, name, mode, sender, connection):
if not self.is_systemd_running:
raise dbus.DBusException, self.msg['not_running']
- check_authorization(sender, connection, self.__action)
+ checkAuthorization(sender, connection, self.__action)
return self.__systemd_interface.RestartUnit(name, mode)
View
176 mcc2/backends/users/service.py
@@ -4,9 +4,9 @@
import dbus.service
import dbus.mainloop.glib
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
-import math
+#import math
import libuser
-from datetime import date, timedelta
+#from datetime import date, timedelta
from mcc2.backends.policykit import checkAuthorization
#TODO: make a method to this
@@ -101,14 +101,14 @@ def AddUser(self, user_info, sender, connection):
Example:
user_info = {
- 'fullname': 'John Doe',
- 'username': 'john',
- 'shell': '/bin/bash',
+ 'fullName': 'John Doe',
+ 'userName': 'john',
+ 'loginShell': '/bin/bash',
'uid': 666,
'gid': 666,
- 'home_directory': '/home/john',
+ 'homeDirectory': '/home/john',
'password': 'secret',
- 'create_home': True
+ 'createHome': True
}
@raise dbus.DBusException:
@@ -119,25 +119,24 @@ def AddUser(self, user_info, sender, connection):
#checkAuthorization(sender, connection,
# 'org.mandrivalinux.mcc2.users.adduser')
- user_entity = self.__libuser.initUser(user_info['username'])
+ user_entity = self.__libuser.initUser(user_info['userName'])
# libuser won't respect that always the fullname will be set by username
# if a fullname was not set.
- if user_info.has_key('fullname'):
- print 'has key fullname'
- user_entity.set(libuser.GECOS, [user_info['fullname']])
+ if user_info.has_key('fullName'):
+ user_entity.set(libuser.GECOS, [user_info['fullName']])
if user_info.has_key('gid'):
user_entity.set(libuser.GIDNUMBER, [user_info['gid']])
user_entity.set(libuser.UIDNUMBER, [user_info['uid']])
- user_entity.set(libuser.HOMEDIRECTORY, [user_info['home_directory']])
- user_entity.set(libuser.LOGINSHELL, [user_info['shell']])
+ user_entity.set(libuser.HOMEDIRECTORY, [user_info['homeDirectory']])
+ user_entity.set(libuser.LOGINSHELL, [user_info['loginShell']])
try:
self.__libuser.addUser(
user_entity,
- mkhomedir = user_info['create_home'])
+ mkhomedir = user_info['createHome'])
except RuntimeError, error:
msg = 'org.mandrivalinux.mcc2.Users.Error.AddUserFailed'
@@ -149,7 +148,7 @@ def AddUser(self, user_info, sender, connection):
self.__libuser.setpassUser(user_entity, user_info['password'], 0)
- user_entity = self.__libuser.lookupUserByName(user_info['username'])
+ user_entity = self.__libuser.lookupUserByName(user_info['userName'])
uid = user_entity.get(libuser.UIDNUMBER)[0]
return dbus.Int64(uid)
@@ -416,7 +415,7 @@ def LockGroup(self, groupname, sender, connection):
out_signature='i',
sender_keyword='sender',
connection_keyword='connection')
- def UnlockGroup(self, groupname, sender, connection):
+ def UnLockGroup(self, groupname, sender, connection):
""" Unlock group
@param groupname: A dbus.String with group name as it value.
@@ -479,7 +478,7 @@ def LockUser(self, username, sender, connection):
out_signature='i',
sender_keyword='sender',
connection_keyword='connection')
- def UnlockUser(self, username, sender, connection):
+ def UnLockUser(self, username, sender, connection):
""" Unlock user
@param username: A dbus.String with user name as it value.
@@ -515,7 +514,7 @@ def ModifyGroup(self, groupInfo, sender, connection):
Example:
groupInfo = {
'oldGroupName': 'john',
- 'newGroupName': 'john'
+ 'newGroupName': 'johns'
'members': ['john', 'users', 'wheel']
}
@@ -527,10 +526,13 @@ def ModifyGroup(self, groupInfo, sender, connection):
groupEntity = self.__libuser.lookupGroupByName(groupInfo['oldGroupName'])
- if groupEntity.has_key('members'):
+ if groupInfo.has_key('members'):
+ if groupInfo['members'][0] == '':
+ groupInfo['members'] = []
groupEntity.set(libuser.MEMBERNAME, groupInfo['members'])
- groupEntity.set(libuser.GROUPNAME, groupInfo['newGroupName'])
+ if groupInfo.has_key('newGroupName'):
+ groupEntity.set(libuser.GROUPNAME, groupInfo['newGroupName'])
return dbus.Int32(self.__libuser.modifyGroup(groupEntity))
@@ -556,20 +558,20 @@ def ModifyUser(self, user_info, sender, connection):
Example:
user_info = {
- 'username': 'john'
- 'new_username': 'johns',
- 'fullname': 'Johns Does',
- 'shell': '/bin/bash',
+ 'oldUserName': 'john'
+ 'newUserName': 'johns',
+ 'fullName': 'Johns Does',
+ 'loginShell': '/bin/bash',
'uid': 666,
'gid': 666,
- 'home_directory': '/home/john',
+ 'homeDirectory': '/home/john',
'password': 'secret2',
- 'shadow_expire': 'YYYY-MM-DD',
- 'shadow_min': 0,
- 'shadow_max', 99999,
- 'shadow_warning': 7,
- 'shadow_inactive': -1,
- 'shadow_last_change': 'YYYY-MM-DD'
+ 'shadowExpire': '15071', # in days
+ 'shadowMin': 0,
+ 'shadowMax', 99999,
+ 'shadowWarning': 7,
+ 'shadowInactive': -1,
+ 'shadowLastChange': '15071' # in days
}
@raise dbus.DBusException:
@@ -582,97 +584,84 @@ def ModifyUser(self, user_info, sender, connection):
#checkAuthorization(sender, connection,
# 'org.mandrivalinux.mcc2.users.modifyuser')
- user_entity = self.__libuser.lookupUserByName(user_info['username'])
- user_entity.set(libuser.GECOS, [user_info['fullname']])
+ user_entity = self.__libuser.lookupUserByName(user_info['oldUserName'])
- #TODO: Check if uid and gid can be converted to int()
- # if not raise an error
- user_entity.set(libuser.GIDNUMBER, [user_info['gid']])
- user_entity.set(libuser.UIDNUMBER, [user_info['uid']])
- user_entity.set(libuser.HOMEDIRECTORY, [user_info['home_directory']])
- user_entity.set(libuser.LOGINSHELL, [user_info['shell']])
-
- if user_info.has_key('new_username'):
- user_entity.set(libuser.USERNAME, [user_info['new_username']])
+ if user_info.has_key('fullName'):
+ user_entity.set(libuser.GECOS, [user_info['fullName']])
- if user_info.has_key('shadow_expire'):
- year = None
- month = None
- day = None
-
- try:
- (year, month, day) = user_info['shadow_expire'].split('-')
- except ValueError:
- msg = 'org.mandrivalinux.mcc2.Users.Error.InvalideDate'
- raise dbus.DBusException, msg
-
- try:
- tmp = time.mktime ([year, month, day, 0, 0, 0, 0, 0, -1])
- except OverflowError:
- msg = 'org.mandrivalinux.mcc2.Users.Error.YearIsTooBig'
- raise dbus.DBusException, msg
+ #TODO: Check if gid can be converted to int()
+ # if not raise an error
+ if user_info.has_key('gid'):
+ user_entity.set(libuser.GIDNUMBER, [user_info['gid']])
+ # Change UID is not permited
+ #user_entity.set(libuser.UIDNUMBER, [user_info['uid']])
+ if user_info.has_key('homeDirectory'):
+ user_entity.set(libuser.HOMEDIRECTORY, [user_info['homeDirectory']])
- seconds = 24 * 60 * 60
- days_expire = tmp / seconds
- fraction, integer = math.modf(days_expire)
+ if user_info.has_key('loginShell'):
+ user_entity.set(libuser.LOGINSHELL, [user_info['loginShell']])
- if fraction == 0.0:
- days_expire = integer
- else:
- days_expire = integer + 1
+ if user_info.has_key('newUserName'):
+ user_entity.set(libuser.USERNAME, [user_info['newUserName']])
- user_entity.set(libuser.SHADOWEXPIRE, days_expire)
+ if user_info.has_key('shadowExpire'):
+ user_entity.set(libuser.SHADOWEXPIRE, [user_info['shadowExpire']])
- if user_info.has_key('shadow_min'):
- user_entity.set(libuser.SHADOWMIN, int(user_info['shadow_min']))
+ if user_info.has_key('shadowMin'):
+ user_entity.set(libuser.SHADOWMIN, int(user_info['shadowMin']))
- if user_info.has_key('shadow_max'):
- user_entity.set(libuser.SHADOWMAX, int(user_info['shadow_max']))
+ if user_info.has_key('shadowMax'):
+ user_entity.set(libuser.SHADOWMAX, int(user_info['shadowMax']))
- if user_info.has_key('shadow_warning'):
+ if user_info.has_key('shadowWarning'):
user_entity.set(
libuser.SHADOWWARNING,
- int(user_info['shadow_warning']))
+ int(user_info['shadowWarning']))
- if user_info.has_key('shadow_inactive'):
+ if user_info.has_key('shadowInactive'):
user_entity.set(
libuser.SHADOWINACTIVE,
- int(user_info['shadow_inactive']))
+ int(user_info['shadowInactive']))
#if user_info.has_key('shadow_last_change'):
# user_entity.set(
# libuser.SHADOWLASTCHANGE,
# int(user_info['shadow_last_change']))
- self.__libuser.setpassUser(user_entity, user_info['password'], 0)
+ if user_info.has_key('password'):
+ self.__libuser.setpassUser(user_entity, user_info['password'], 0)
return self.__libuser.modifyUser(user_entity)
@dbus.service.method("org.mandrivalinux.mcc2.Users",
- in_signature='s',
+ in_signature='s',
out_signature='a{sv}')
- def UserDetails(self, username):
+ def UserDetails(self, userName):
"""
shadow_expire: return -1 if expiration is disabled.
"""
- user_entity = self.__libuser.lookupUserByName(username)
+ userEntity = self.__libuser.lookupUserByName(userName)
+
+ groups = self.__libuser.enumerateGroupsByUser(userName)
+ if not groups:
+ groups = ['']
return dbus.Dictionary(
{
- 'uid': user_entity.get(libuser.UIDNUMBER)[0],
- 'gid': user_entity.get(libuser.GIDNUMBER)[0],
- 'username': user_entity.get(libuser.USERNAME)[0],
- 'fullname': user_entity.get(libuser.GECOS)[0],
- 'home_directory': user_entity.get(libuser.HOMEDIRECTORY)[0],
- #'groups': self.__libuser.enumerateGroupsByUser(username),
- 'login_shell': user_entity.get(libuser.LOGINSHELL)[0],
- 'shadow_expire': user_entity.get(libuser.SHADOWEXPIRE)[0],
- 'shadow_min': user_entity.get(libuser.SHADOWMIN)[0],
- 'shadow_max': user_entity.get(libuser.SHADOWMAX)[0],
- 'shadow_warning': user_entity.get(libuser.SHADOWWARNING)[0],
- 'shadow_inactive': user_entity.get(libuser.SHADOWINACTIVE)[0],
- 'shadow_last_change': user_entity.get(libuser.SHADOWLASTCHANGE)[0],
+ 'uid': userEntity.get(libuser.UIDNUMBER)[0],
+ 'gid': userEntity.get(libuser.GIDNUMBER)[0],
+ 'userName': userEntity.get(libuser.USERNAME)[0],
+ 'fullName': userEntity.get(libuser.GECOS)[0],
+ 'homeDirectory': userEntity.get(libuser.HOMEDIRECTORY)[0],
+ 'groups': groups,
+ 'loginShell': userEntity.get(libuser.LOGINSHELL)[0],
+ 'shadowExpire': userEntity.get(libuser.SHADOWEXPIRE)[0],
+ 'shadowMin': userEntity.get(libuser.SHADOWMIN)[0],
+ 'shadowMax': userEntity.get(libuser.SHADOWMAX)[0],
+ 'shadowWarning': userEntity.get(libuser.SHADOWWARNING)[0],
+ 'shadowInactive': userEntity.get(libuser.SHADOWINACTIVE)[0],
+ 'shadowLastChange': userEntity.get(libuser.SHADOWLASTCHANGE)[0],
}, signature=dbus.Signature('sv'))
@@ -682,12 +671,15 @@ def UserDetails(self, username):
def GroupDetails(self, groupName):
groupEntity = self.__libuser.lookupGroupByName(groupName)
+ members = self.__libuser.enumerateUsersByGroup(groupName)
+ if not members:
+ members = ['']
return dbus.Dictionary(
{
'gid': groupEntity.get(libuser.GIDNUMBER)[0],
'groupName': groupEntity.get(libuser.GROUPNAME)[0],
- 'members': self.__libuser.enumerateUsersByGroup(groupName)
+ 'members': members
}, signature=dbus.Signature('sv'))
View
13 mcc2/frontends/users/controller.py
@@ -30,7 +30,8 @@ def addUser(self, grid):
user_info['fullname'] = fullname
userName = grid.findChild(QtDeclarative.QDeclarativeItem, 'addUserName')
- user_info['username'] = userName.property('text')
+ user_info['userName'] = userName.property('text')
+
password = grid.findChild(QtDeclarative.QDeclarativeItem, 'addPassword')
confirmPassword = grid.findChild(QtDeclarative.QDeclarativeItem, 'addConfirmPassword')
#TODO Find a way to show a message to gui from here
@@ -40,18 +41,18 @@ def addUser(self, grid):
print "Password mismatch!"
loginShell = grid.findChild(QtDeclarative.QDeclarativeItem, 'addLoginShell')
- user_info['shell'] = loginShell.property('text')
+ user_info['loginShell'] = loginShell.property('text')
createHomeDirectory = grid.findChild(QtDeclarative.QDeclarativeItem, 'addCreateHomeDirectory')
- user_info['create_home'] = createHomeDirectory.property('checked')
+ user_info['createHome'] = createHomeDirectory.property('checked')
homeDirectory = grid.findChild(QtDeclarative.QDeclarativeItem, 'addHomeDirectory')
- user_info['home_directory'] = homeDirectory.property('text')
+ user_info['homeDirectory'] = homeDirectory.property('text')
privateGroup = grid.findChild(QtDeclarative.QDeclarativeItem, 'addCreatePrivateGroup')
if privateGroup.property('checked'):
gid = self.__interface.FirstUnusedGid()
#TODO: Add exception handler here
- user_info['gid'] = self.__interface.AddGroup(user_info['username'], gid)
- self.parent.groupModel.addGroup(user_info['username'])
+ user_info['gid'] = self.__interface.AddGroup(user_info['userName'], gid)
+ self.parent.groupModel.addGroup(user_info['userName'])
user_info['uid'] = self.__interface.FirstUnusedUid()
specifyUserId = grid.findChild(QtDeclarative.QDeclarativeItem, 'addSpecifyUserId')
View
22 mcc2/frontends/users/models.py
@@ -25,24 +25,24 @@ def __init__(self, user):
self.__expiration_date = ''
self.__shadow_expire = False
- if self.__userDetails['shadow_expire'] > 0:
+ if self.__userDetails['shadowExpire'] > 0:
self.__shadow_expire = True
- self.__expiration_date = self.__userDetails['shadow_expire']
+ self.__expiration_date = self.__userDetails['shadowExpire']
def _uid(self):
return str(self.__userDetails['uid'])
def _username(self):
- return str(self.__userDetails['username'])
+ return str(self.__userDetails['userName'])
def _fullname(self):
- return str(self.__userDetails['fullname'])
+ return str(self.__userDetails['fullName'])
def _home_directory(self):
- return str(self.__userDetails['home_directory'])
+ return str(self.__userDetails['homeDirectory'])
def _login_shell(self):
- return str(self.__userDetails['login_shell'])
+ return str(self.__userDetails['loginShell'])
def _shadow_expire(self):
return self.__shadow_expire
@@ -51,19 +51,19 @@ def _expiration_date(self):
return str(self.__expiration_date)
def _shadow_min(self):
- return str(self.__userDetails['shadow_min'])
+ return str(self.__userDetails['shadowMin'])
def _shadow_max(self):
- return str(self.__userDetails['shadow_max'])
+ return str(self.__userDetails['shadowMax'])
def _shadow_warning(self):
- return str(self.__userDetails['shadow_warning'])
+ return str(self.__userDetails['shadowWarning'])
def _shadow_inactive(self):
- return str(self.__userDetails['shadow_inactive'])
+ return str(self.__userDetails['shadowInactive'])
def _shadow_last_change(self):
- return str(self.__userDetails['shadow_last_change'])
+ return str(self.__userDetails['shadowLastChange'])
def _groups(self):
return self.__groups
Please sign in to comment.
Something went wrong with that request. Please try again.