您好,登錄后才能下訂單哦!
在腳本的同目錄下需要創建一個名為“useradd.txt”的文件,用于存放需要創建的用戶,useradd的內容格式:賬號,郵箱,密碼(注意,這3個單元之間要以英文逗號隔開),
舉例,我要新建一個賬號為test,郵箱為test@qq.com,密碼為12345678的用戶,useradd.txt的格式如下:
test,test@qq.com,12345678
整個代碼包含以下功能
1、創建用戶并賦予權限
2、刪除用戶
3、更新指定組內的用戶權限
4、將指定組內的部分用戶權限去除,運行界面如下
代碼內容如下:
#C:\Python27
# coding=utf-8
import gitlab,re
def add(gl):
gid = int(raw_input('Group ID: '))
group = gl.groups.get(gid)
print group.name
mkey = []
path = 'useradd.txt'
with open(path,'r') as f:
for fnun in f.readlines():
fkey = fnun.strip()
name = fkey.split(',')[0]
mail = fkey.split(',')[1]
username = mail.split('@')[0]
passwd = fkey.split(',')[2]
user = gl.users.create({'email':mail,'password':passwd,'username':username ,'name':name})
print user.username,user.id
mkey.append(user.id)
for i in mkey:
member = group.members.create({'user_id':i,'access_level':30})
print member.username,member.access_level
######del user#######
def deluser(gl):
#cancel = [153, 151, 149, 147, 91, 51, 49, 47, 45, 43, 41, 39, 37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 17, 15]
num = raw_input('Inout the user_id: ') #輸入需要刪除的用戶的ID
cancel = []
cancel.append(num)
for m in cancel:
user = gl.users.get(m)
uname = user.name
print 'Useri_id:%s , name:%s has been erased' % (m,uname)
gl.users.delete(m)
######add user to group#######
def update(gl):
gid = int(raw_input('Group ID: '))
group = gl.groups.get(gid)
print group.name
fnum = [139, 137, 127, 125, 123, 121, 117, 115, 113, 109, 105, 101, 97, 93, 87, 79, 63, 61, 59] #需要更新權限的用戶ID
for i in fnum:
member = group.members.create({'user_id':i,'access_level':30})
print member.username,member.access_level
######remove user from group#######
def remove(gl):
cancel = [51, 49, 47, 45, 43, 41, 39, 37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 17, 15] #刪除指定組內的用戶權限的ID
gid = int(raw_input('Group ID: '))
group = gl.groups.get(gid)
print group.name
#num = raw_input('Inout the user_id: ')
#cancel = []
#cancel.append(num)
for m in cancel:
user = gl.users.get(m)
uname = user.name
print 'Useri_id:%s , name:%s will be removed' % (m,uname)
member = group.members.delete(m)
mem = group.members.list(all=True)
for me in mem:
print me.id,me.username
def main():
print '''%s
1. 增加git用戶并賦予權限
2. 刪除git用戶
3. 更新git用戶權限
4. 從指定的組中刪除用戶權限
%s ''' % ('-'*20,'-'*20)
gl = gitlab.Gitlab('http://10.50.36.33/', private_token='ffffffffffffffffj')
info = {1:'add(gl)',2:'deluser(gl)',3:'update(gl)',4:'remove(gl)'}
num = int(raw_input('Input your choice: '))
exec info[num]
if __name__ == '__main__':
main()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。