def get_grants(self, globals_privs=False): """Retrieve the grants for the current user globals_privs[in] Include global privileges in clone (i.e. user@%) returns result set or None if no grants defined """ # Get the users' connection user@host if not retrieved if self.current_user is None: res = self.server1.exec_query("SELECT CURRENT_USER()") parts = res[0][0].split('@') # If we're connected as some other user, use the user@host # defined at instantiation if parts[0] != self.user: host = clean_IPv6(self.host) self.current_user = "******" % (self.user, host) else: self.current_user = "******" % (parts[0], parts[1]) grants = [] try: res = self.server1.exec_query("SHOW GRANTS FOR " "{0}".format(self.current_user)) for grant in res: grants.append(grant) except UtilDBError: pass # Error here is ok - no grants found. if globals_privs: try: res = self.server1.exec_query("SHOW GRANTS FOR " "'{0}'{1}".format(self.user, "@'%'")) for grant in res: grants.append(grant) except UtilDBError: pass # Error here is ok - no grants found. return grants
def get_grants(self, globals_privs=False, as_dict=False, refresh=False): """Retrieve the grants for the current user globals_privs[in] Include global privileges in clone (i.e. user@%) as_dict[in] If True, instead of a list of plain grant strings, return a dictionary with the grants. refresh[in] If True, reads grant privileges directly from the server and updates cached values, otherwise uses the cached values. returns result set or None if no grants defined """ # only read values from server if needed if refresh or not self.grant_list or not self.global_grant_list: # Get the users' connection user@host if not retrieved if self.current_user is None: res = self.server1.exec_query("SELECT CURRENT_USER()") parts = res[0][0].split('@') # If we're connected as some other user, use the user@host # defined at instantiation if parts[0] != self.user: host = clean_IPv6(self.host) self.current_user = "******" % (self.user, host) else: self.current_user = "******" % (parts[0], parts[1]) grants = [] try: res = self.server1.exec_query("SHOW GRANTS FOR " "{0}".format(self.current_user)) for grant in res: grants.append(grant) except UtilDBError: pass # Error here is ok - no grants found. # Cache user grants self.grant_list = grants[:] self.grant_dict = User._get_grants_as_dict(self.grant_list, self.verbosity) # If current user is already using global host wildcard '%', there # is no need to run the show grants again. if globals_privs: if self.host != '%': try: res = self.server1.exec_query( "SHOW GRANTS FOR '{0}'{1}".format(self.user, "@'%'")) for grant in res: grants.append(grant) self.global_grant_list = grants[:] self.global_grant_dict = User._get_grants_as_dict( self.global_grant_list, self.verbosity) except UtilDBError: # User has no global privs, return the just the ones # for current host self.global_grant_list = self.grant_list self.global_grant_dict = self.grant_dict else: # if host is % then we already have the global privs self.global_grant_list = self.grant_list self.global_grant_dict = self.grant_dict if globals_privs: if as_dict: return self.global_grant_dict else: return self.global_grant_list else: if as_dict: return self.grant_dict else: return self.grant_list