def user_create_new(username, pwd, role): result = {} output = 1 try: auth_binary = get_authentication_library() if user_id_from_name(username) is not None: return set_failure_dict("User already exists", completion_code.failure) if len(username) < 6: return set_failure_dict( "Username needs to be at least 6 characters", completion_code.failure) username = c_char_p(username) pwd = c_char_p(pwd) role = c_char_p(role) output = auth_binary.add_user(username, role, pwd) if output is not 0: return set_failure_dict( ("Failed to execute command using libocsauth", output), completion_code.failure) except Exception, e: return set_failure_dict(("user_create_new() Exception ", e), completion_code.failure)
def user_update_role(username, role): result = {} output = 1 try: auth_binary = get_authentication_library() if user_id_from_name(username) is None: return set_failure_dict("User does not exist", completion_code.failure) username = c_char_p(username) role = c_char_p(role) output = auth_binary.ocs_change_role(username, role) if output is not 0: return set_failure_dict( ("Failed to execute command using libocsauth", output), completion_code.failure) except Exception, e: return set_failure_dict(("user_update_role() Exception ", e), completion_code.failure)
def user_delete_by_name(username): result = {} output = 1 try: auth_binary = get_authentication_library() if user_id_from_name(username) is None: return set_failure_dict("User does not exist", completion_code.failure) if (username != 'root'): username = c_char_p(username) else: return set_failure_dict("Removing root user not allowed", completion_code.failure) output = auth_binary.remove_user(username) if output is not 0: return set_failure_dict( ("Failed to execute command using libocsauth", output), completion_code.failure) except Exception, e: return set_failure_dict(("user_delete_by_name() Exception ", e), completion_code.failure)
def group_name_from_usr_name(username): try: auth_binary = get_authentication_library() usr = c_char_p(username) gname = create_string_buffer(20) ret = auth_binary.get_user_group_name(usr, 20, byref(gname)) if ret == 0: return gname.value else: return "unknown user" except Exception, e: print("group_name_from_usr_name() Exception {0}".format(e)) return "failed"
def group_id_from_usr_name(username): try: auth_binary = get_authentication_library() usr = c_char_p(username) gpid = c_int(-1) ret = auth_binary.get_user_group_id(usr, byref(gpid)) if ret == 0: return gpid.value else: return -1 except Exception, e: print("group_id_from_usr_name() Exception {0}".format(e)) return -1
def group_id_from_usr_name(username): try: auth_binary = get_authentication_library() usr = c_char_p(username) gpid = c_int(-1) ret = auth_binary.get_user_group_id(usr, byref(gpid)) if ret == 0: return gpid.value else: print("unknown user: unable to locate user Id") return -1 except Exception, e: print("group_id_from_usr_name() Exception ", e) return -1
def user_id_from_name(username): try: auth_binary = get_authentication_library() usr = c_char_p(username) uid = c_uint() ret = auth_binary.get_userid_from_name(usr, byref(uid)) if ret == 0: return uid.value else: return None except Exception, e: print("user_id_from_name() Exception {0}".format(e)) return None
def user_detail(username, usr_id, usr_role): auth_binary = get_authentication_library() role_name = create_string_buffer(20) ret = get_groupname_from_id(usr_role, 20, byref(role_name)) return { "Id": usr_id, "Description": "Ocs {0} account".format(role_name.value), "Enabled": getUserAccountStatus(username), "UserName": username, "RoleId": role_name.value, "Locked": "False", completion_code.cc_key: completion_code.success }
def user_name_from_group_name(gp_name): try: auth_binary = get_authentication_library() group = c_char_p(gp_name) members = create_string_buffer(220) i = c_int(220) ret = auth_binary.ocs_group_members(group, byref(i), byref(members)) if ret == 0: return members.value else: return '' except Exception, e: print("user_name_from_group_name() Exception {0}".format(e)) return ''
def get_group_id_by_name(group_name): id=0 try: g_name=c_char_p(group_name) g_id = c_int() auth_binary = get_authentication_library () ret = auth_binary.ocs_group_id(g_name, byref(g_id)) if ( ret ==0 ): id = g_id.value except: #log error message pass return id