Reputation: 51
Following situation:
#!/bin/bash
lpstat -p | awk '/^printer/ {print $2}' | while read printer
do
echo "Deleting Printer/Class:" $printer
lpadmin -U $SUDO_USER -x $printer
done
My problem is, that all CUPS printers will be reconfigured when I switch the user. Is there a function to bind the CUPS printers to a specific user and only clean those printers which belong to the user?
Example: User1 logs in with printer dev1, dev2 and dev3. Now he switches to a new session with User2 who only has the dev1 printer. Now dev2 and dev3 will be deleted for both users and after switching back to User1 dev2 and 3 aren't existent.
Thanks in advance!
Greetings
Stefan
EDIT: Found an answer! Will post it in the afternoon when I have time to document everything.
EDIT2: Answer is posted ;)
Upvotes: 1
Views: 561
Reputation: 51
I found an answer for the question:
I use a function to find every user who is logged in and use all the yaml-files of each logged in user (the userconfig.yaml is permanently saved and gets an update during login).
# using self made module system.current_users()
users = system.current_users()
printers_with_users = {}
for username in users:
try:
printer_yaml = config_data.retrieve_for(username).get(u"printers", [])
except:
print("Aborting: Can't find userconfig.yaml!\n")
exit(1)
for device, dev_list in printer_yaml.iteritems():
# parsing datas...
config_classes = dev_list.get("config_classes", None)
descr = get_unicode(dev_list, u"descr")
printer_id = get_unicode(dev_list, u"id")
is_local = dev_list.get("is_local", None)
listen_network = dev_list.get("listen_network", None)
location = get_unicode(dev_list, u"location")
ppd_file = get_unicode(dev_list, u"ppd_file")
ppd_uri = get_unicode(dev_list, u"ppd_uri")
printer_uri = get_unicode(dev_list, u"printer_uri")
# here is the cool part ;)
printers_with_users.setdefault(printer_id, []).append(username)
#configuring printers...
# configure access rules in printers.conf
for printers, accepted_users in printers_with_users.iteritems():
users_string = ",".join(accepted_users)
try:
subproc.call('lpadmin', ['-p', printers, '-u', 'allow:%s' % users_string])
except:
print ( " - unable to set user access rules\n" )
print (config_error) % printers
continue
I'm doing the same thing for the classes, too.
I can still clean the whole config file during each login and can also assure that every user can use his own printers.
Hopefully this is helpful for everyone who has the same problem (even though it is only a small piece of the whole printer manager ;) ).
Upvotes: 1