python active_directive to set attributes - python

How do we set attributes using python active_directive. I have used following to set the mail property for the user, but its not working,
import active_directive
User = active_directory.AD_object ("LocalAdmin").find_user("User1")
print User.sAMAccountName
print User.displayName
print User.mail
User.setattr("mail","User1#testmail.com")
Here. I'm able to get the attributes(displayName, mail) of 'User1'. but not able set the mail attribute by using setattr()
Note : I have full permission to add, remove the user or group into the domain. I'm able to add and remove the group in the domain

We can set email id for the ADUser by using following line,
User.set(mail = "User1#testmail.com")

Related

Marking an email as read python

I'd like to mark an email as read from my python code. I'm using
from exchangelib import Credentials, Account
my_account = Account(...)
credentials = Credentials(...)
to get access to the account. This part works great. I then get into my desired folder using this
var1 = my_account.root / 'branch1' / 'desiredFolder'
Again, this works great. This is where marking it as read seems to not work.
item = var1.filter(is_read=False).values('body')
for i, body in enumerate(item):
#Code doing stuff
var1.filter(is_read=False)[i].is_read = True
var1.filter(is_read=False)[i].save(updated_fields=['is_read'])
I've tried the tips and answers from this post Mark email as read with exchangelib, but the emails still show as unread. What am I doing wrong?
I think you the last line of code that you save() do not work as you think that after you set is_read of unread[i] element to True, this unread[i] of course not appear in var1.filter(is_read=False)[i] again, so you acttually did not save this.
I think this will work.
for msg in my_account.inbox.filter(is_read=False):
msg.is_read = True
msg.save(updated_fields=['is_read'])
To expand on #thangnd's answer, the reason your code doesn't work is that you are calling .save() on a different Python object than the object you are setting the is_read flag on. Every time you call var1.filter(is_read=False)[i], a new query is sent to the server, and a new Python object is created.
Additionally, since you didn't specify a sort order (and new emails may be incoming while the code is running), you may not even be hitting the same email each time you call var1.filter(is_read=False)[i].

Fire Store's on_snapshot() function in Python executes twice. How to make it execute once?

This is my callback that executes when a user is added to a collection
# Create a callback on_snapshot function to capture changes
def on_snapshot_user(col_snapshot, changes, read_time):
print(u'Callback received query snapshot user.')
for change in changes:
if change.type.name == 'ADDED':
doc = change.document.to_dict()
email = doc["email"]
group = doc["survey_group"]
gender = doc["survey_gender"]
age = doc["survey_age"]
userInfo = User.query.filter_by(email=email).first()
if userInfo is None:
User.insert(User(email))
userInfo = User.query.filter_by(email=email).first()
userInfo.group = group
userInfo.gender = gender
userInfo.age = age
User.update(userInfo)
email is a primary key. When a user is added to Firestore, this triggers twice and it gives an error of a duplicate key.
How do I make this execute once in Python as metadata.PendingWrites is not yet supported in Python?
I am developing in Flask. I'm new to it so would appreciate any kind of help.
Edit:
Some context - I am adding the data I get to a PostgreSQL database. I am planning to do so as I need to build a kind of leaderboard and I'm planning to store the user info + their points in a postgreSQL table.
There are some steps to resolve multiple execution issue :
1. First would be to create a doc reference variable for the document you want to execute.
Create a doc_ref variable for the document you want to populate.
Syntax : doc_ref = db.Collection(collection_name)
2. Second step would be to watch the document.
doc_watch would be the variable created for watching the document.
Syntax : doc_watch = doc_ref.on_snapshot_functionName(on_snapshot_functionName)
3. Third step would be to Terminate watch on the document so it will be executed once.
You can use unsubscribe() to terminate the watch.
Syntax : doc_watch.unsubscribe()
For further details related to real time updates in Cloud Firestore, you can refer to the documentation [1].
[1] : https://cloud.google.com/firestore/docs/query-data/listen

How to send current user_id to Sentry scope.user()?

Project is in PYTHON
I want to log all current users affected by some random "IndexError" (For instance) error.
Currently when I try to do this:
from sentry_sdk import push_scope, capture_exception
with push_scope() as scope:
scope.user = {"email": user.email}
capture_exception(ex)
it sets first user's email and that doesn't change with other users facing the same error.
I want to see all users who were affected by that error.

How to find all the groups the user is a member? (LDAP)

I am trying to get all the groups that a certain user is a member of.
I have the following structures in ldap:
o=myOrganization
ou=unit1
cn=admin
cn=guess
and
ou=users
cn=ann
cn=bob
cn=carla
myOrganization is an instance of Organization
unit1 is an instance of OrganizationUnit
admin and guess are both GroupOfNames and have everyone as a member
ann, bob, and carla are instances of Person
Currently, I am using the ldap module on python and this is what I have:
import ldap
l = ldap.initialize("ldap://my_host")
l.simple_bind_s("[my_dn]", "[my_pass]")
ldap_result = l.search("[BASE_DN]", ldap.SCOPE_SUBTREE, "(&(objectClass=Person)(cn=ann))", None)
res_type, data = l.result(ldap_result, 0)
print(data)
And I am able to get the user ann; but, how do I go about getting the groups Ann belongs to?
I tried, the following from this page:
search_filter='(|(&(objectClass=*)(member=cn=ann)))'
results = l.search_s([BASE_DN], ldap.SCOPE_SUBTREE, search_filter, ['cn',])
But I got an empty list. I also tried various combinations of queries, but they all return empty.
PS: I am using OpenLDAP on a linux machine
member=cn=ann is not enough. You have to use ann's full DN, probably something like this:
member=cn=ann,ou=users,dc=company,dc=com

How do you escape a dash in Jython/Websphere?

I have a Jython script that is used to set up a JDBC datasource on a Websphere 7.0 server. I need to set several properties on that datasource. I am using this code, which works, unless value is '-'.
def setCustomProperty(datasource, name, value):
parms = ['-propertyName', name, '-propertyValue', value]
AdminTask.setResourceProperty(datasource, parms)
I need to set the dateSeparator property on my datasource to just that - a dash. When I run this script with setCustomProperty(ds, 'dateSeparator', '-') I get an exception that says, "Invalid property: ". I figured out that it thinks that the dash means that another parameter/argument pair is expected.
Is there any way to get AdminTask to accept a dash?
NOTE: I can't set it via AdminConfig because I cannot find a way to get the id of the right property (I have multiple datasources).
Here is a solution that uses AdminConfig so that you can set the property value to the dash -. The solution accounts for multiple data sources, finding the correct one by specifying the appropriate scope (i.e. the server, but this could be modified if your datasource exists within a different scope) and then finding the datasource by name. The solution also accounts for modifying the existing "dateSeparator" property if it exists, or it creates it if it doesn't.
The code doesn't look terribly elegant, but I think it should solve your problem :
def setDataSourceProperty(cell, node, server, ds, propName, propVal) :
scopes = AdminConfig.getid("/Cell:%s/Node:%s/Server:%s/" % (cell, node, server)).splitlines()
datasources = AdminConfig.list("DataSource", scopes[0]).splitlines()
for datasource in datasources :
if AdminConfig.showAttribute(datasource, "name") == ds :
propertySet = AdminConfig.list("J2EEResourcePropertySet", datasource).splitlines()
customProp = [["name", propName], ["value", propVal]]
for property in AdminConfig.list("J2EEResourceProperty", propertySet[0]).splitlines() :
if AdminConfig.showAttribute(property, "name") == propName :
AdminConfig.modify(property, customProp)
return
AdminConfig.create("J2EEResourceProperty", propertySet[0], customProp)
if (__name__ == "__main__"):
setDataSourceProperty("myCell01", "myNode01", "myServer", "myDataSource", "dateSeparator", "-")
AdminConfig.save()
Please see the Management Console preferences settings. You can do what you are attempting now and you should get to see the Jython equivalent that the Management Console is creating for its own use. Then just copy it.
#Schemetrical solution worked for me. Just giving another example with jvm args.
Not commenting on the actual answer because I don't have enough reputation.
server_name = 'server1'
AdminTask.setGenericJVMArguments('[ -serverName %s -genericJvmArguments "-agentlib:getClasses" ]' % (server_name))
Try using a String instead of an array to pass the parameters using double quotes to surround the values starting with a dash sign
Example:
AdminTask.setVariable('-variableName JDK_PARAMS -variableValue "-Xlp -Xscm250M" -variableDescription "-Yes -I -can -now -use -dashes -everywhere :-)" -scope Cell=MyCell')

Categories

Resources