I've made a python function that returns the user logged in a postgis layer:
from qgis.core import * from qgis.gui import * @qgsfunction(args='auto', group='Custom') def getUser(lay, feature, parent): layer=None for lyr in QgsMapLayerRegistry.instance().mapLayers().values(): if lyr.name() == lay: layer = lyr break connInfo = QgsDataSourceURI(layer.dataProvider().dataSourceUri()).connectionInfo() success, username, password = QgsCredentials.instance().get(connInfo, None, None) if success: return username
The problem is that the result of the function seems to be ok in the output preview when creating a new calculated field:
Not only that but also keeps asking me to insert the credentials for the layer again and again...
The credentials are not stored in the layer so every time I open the map I am prompted to insert them. I did it just once in the beginning until now.
What am I doing wrong?