Setting posting and receving attributes when adding member to a group - python

After adding a member to a group:
groupMember = {
"email": sender,
"role": "MEMBER",
}
directory_service.members().insert(groupKey=groupName,body=groupMember).execute()
I would like to set the following attributes for that same member:
- Email delivery (we want to set it to no mail)
- Posting permission (we want to set it to "Override: member is allowed to post")
I can't find python (or other languages) API for setting group member attributes other than these member resource attributes:
{
"kind": "admin#directory#member",
"etag": etag,
"id": string,
"email": string,
"role": string,
"type": string
}
Is there an API (python or other languages) to set "Email delivery" and "Posting permission" attributes for a specific group member?
Thank you for your help!

There is no API to set group member email preferences.

Related

Tweepy get_bookmarks() - User Fields Not Working Despite Proper Arguments

I am using the tweepy library to retrieve user bookmarks, but I am unable to retrieve the profile picture URL despite passing the correct user_field.
According to the tweepy documentation, the only available expansion for this endpoint is "expansions=owner_id". I am not sure what this means or how to retrieve the profile picture URL.
Can someone help me understand this and provide a solution?
bookmarks = client.get_bookmarks(expansions=["author_id"],
user_fields=["profile_image_url"])
The response object is as follows:
{
"data": [
{
"id": 1621356248334483456,
"text": "The majesty of nature is on full display at Yosemite National Park. Visit now to experience it for yourself: https://natureloversparadise.com"
},
{
"id": 1620813010569945091,
"text": "Discover the beauty of the Great Barrier Reef with a snorkeling adventure. Book now at https://greatbarrierreefadventures.com"
}
],
"includes": {
"users": [
{
"id": 123456789,
"username": "naturelover",
"name": "Nature Enthusiast"
},
{
"id": 987654321,
"username": "oceanadventurer",
"name": "Ocean Adventurer"
}
]
}
}
As you can see, the includes section only contains basic information about the authors of the tweets (id , username , and name ), and no information about profile pictures.
Thank you for your help.
I have successfully made the necessary scope access and authentication. However, I think the get_bookmark() method might only return basic author information like author_id user fields and need confirmation before using user_id to call user information from different method.

Modifying the Employee Information field in Suite Admin SDK Python

Is there a way to modify these fields through the Admin SDK without manually doing it through the admin console? Specifically under the employee information, the job title, type of employee, manager's email and department.
I've looked at https://developers.google.com/admin-sdk/directory/reference/rest/v1/users and https://googleapis.github.io/google-api-python-client/docs/dyn/admin_directory_v1.users.html#insert but I don't see any parameter that you can specify these fields in.
Any help would be appreciated, thank you!
The fields are kind of scattered around but we can figure it out by filling them manually on a test user and running a users.get on them.
So for an employee that looks like this:
The API result is the following (after removing the irrelevant fields):
{
"externalIds": [
{
"value": "ID-123",
"type": "organization"
}
],
"relations": [
{
"value": "example#domain.com",
"type": "manager"
}
],
"organizations": [
{
"title": "My Job Title",
"primary": true,
"customType": "",
"department": "My Department",
"description": "My Employee Type",
"costCenter": "My Cost Center"
}
],
"locations": [
{
"buildingId": "My Building ID",
"floorName": "My Floor Name",
"floorSection": "My Floor Section"
}
]
}
So from this we can deduce that Job Title corresponds to organizations.title, Type of employee corresponds to organizations.description, Manager's email corresponds to relations.value that also has a relations.type = manager and Department corresponds to organizations.department.
Do note that as explained in the documentation that you were looking at, these fields can hold multiple values, so the user can have multiple organizations, but the one that shows in the Admin console is the one marked as primary, they can have multiple relations other than manager such as assistant, spouse, etc. They can have multiple locations and so on.
Generally what will be displayed in the Admin console are the primary fields. Any secondary values may not show up but you can still retrieve them via the API. Most likely they're meant for custom applications that can take advantage of reading these multiple fields, while the console only shows the most relevant fields (as decided by Google).

Edit response object of login in flask-security

I am making a small app which requires login functionality. I am using flask for making api's and flask-security to have login feature.
Login feature is working fine, but I need response object in some customised form.
Right now I am getting response object as:
{
"meta": {
"code": 200
},
"response": {
"user": {
"authentication_token": "some token",
"id": "some id"
}
}
}
I need output in some customised form which will be:
{
"meta": {
"code": 200
},
"response": {
"user": {
"authentication_token": "some token",
"id": "some id",
"role": "some role"
}
}
}
I want role of the user along with response. I saw flask-security-config but didn't get any way to do so.
Is there any way, in which I can get this desired behaviour either by changing some config of flask-security or writing some wrapper function on top of this api.
Any useful suggestion will be very helpful. Thanks!

Django REST framework inconsistent data

When I request my user data from localhost:8000/users, I get users that look like this:
{
"username": "iv4ha05k",
"email": "xxxx#xxx.com",
"profile": {
"image": "http://localhost:8000/media/images-4.jpg"
},
"pk": 303
},
But, if I request user data from the django manage shell, the data will look like this:
{
"username": "iv4ha05k",
"email": "xxxx#xxx.com",
"profile": {
"image": "/media/images-4.jpg"
},
"pk": 303
},
Notice how the image url becomes truncated. I am querying the same exact database in the same exact state, and it has nothing to do with migrations or anything of the like. I discovered this issue when trying to write custom API views. When I would query the User viewset, it would return data with the full image address. But, when I try to write a custom API view that queries the user database, it will return it with the truncated address. I am using django's FileSystemStorage, if that helps anyone.. Thanks.

RESTfully routing an API based on user roles

I am developing an API using Flask-RESTful, and my application has three roles.
site_admin
department_admin
basic
For any given resource, the JSON object returned has a different set of keys based on each role.
For example, if you hit /orders as "site_admin", the result could look like this:
{
"orders": [
{"id": 1, "user": "foo", "paid": True, "department": "A", "code": 456},
{"id": 2, "user": "bar", "paid": False, "department": "A", "code": 567},
{"id": 3, "user": "meh", "paid": False, "department": "B", "code": 678}
]
}
However, if you hit /orders as "department_admin", the result could look like this:
{
"orders": [
{"id": 3, "user": "meh", "paid": False}
]
}
And if you hit /orders as "basic" it would be a very minimal JSON response like this:
{
"orders": [
{"id": 2, "paid": True}
]
}
What is the RESTful way of implementing this?
I can come up with three ways of doing it.
(1) using a request arg and filtering on that:
class Orders(restful.Resource):
def get(self):
if request.args['role'] == 'site_admin':
return admin_JSON_response()
elif request.args['role'] == 'department_admin':
return dept_admin_JSON_response()
else:
return basic_JSON_response()
api.add_resource(Orders, '/orders')
(2) filtering on the session object:
class Orders(restful.Resource):
def get(self):
if session['role'] == 'site_admin':
return admin_JSON_response()
elif session['role'] == 'department_admin':
return dept_admin_JSON_response()
else:
return basic_JSON_response()
api.add_resource(Orders, '/orders')
(3) having a different route for each role:
class OrdersSiteAdmin(restful.Resource):
def get(self):
return admin_JSON_response()
api.add_resource(OrdersSiteAdmin, '/orders_site_admin')
class OrdersDeptAdmin(restful.Resource):
def get(self):
return dept_admin_JSON_response()
api.add_resource(OrdersDeptAdmin, '/orders_dept_admin')
class OrdersBasic(restful.Resource):
def get(self):
return basic_JSON_response()
api.add_resource(OrdersBasic, '/orders_basic')
... Is there a consensus on which is the preferred way RESTfully?
Thanks so much!
Your option #2 violates the the "stateless" constraint, the use of user sessions is not a good idea in a REST API, and instead you should require your clients to provide authentication with every request.
Let's assume you fix #2 and instead of a user session you now have a current_user variable, which is populated during authentication. Then you could rewrite that example as follows:
class Orders(restful.Resource):
def get(self):
if current_user.role == 'site_admin':
return admin_JSON_response()
elif current_user.role == 'department_admin':
return dept_admin_JSON_response()
else:
return basic_JSON_response()
api.add_resource(Orders, '/orders')
Let's look at your three options one by one:
(1) specifies the role in the query string, which would enable any user to request any representation, just by passing the desired role. But why put the role in the query string? I assume you will authenticate your users, so knowing your user you also know the role. This seems unnecessary and will give you extra validation effort.
(3) creates different resources for each role. Once again, you have to ensure that a "basic" user does not have access to the two URLs that apply to the higher roles, so you have also some validation work here.
(2) assumes the user database stores the role of each user, so once the user is authenticated the correct representation for his/her role is returned based on the assigned role. This is, I think, the best option, as users have really no way to hack their way into data they are not allowed to see.
Speaking of being RESTful, I would also look at your representations, which can be improved. Consider implementing links to other resources instead of providing IDs, to comply with the HATEOAS constraint.

Categories