Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions doc/deployer/create_authusers_from_code_username_password_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
with open(file_input, 'rb') as csvfile:
users = csv.reader(csvfile, delimiter=',')
for school_code, username, password in users:
# for school_code, username, password, user_id, first_name, last_name in users:

# email validation from username
email = ''
Expand All @@ -33,6 +34,7 @@

temp_csv_log_list = [school_code, username, password]
user_obj = User.objects.create_user(username=username, email=email, password=password)
# user_obj = User.objects.create_user(username=username, email=email, password=password, first_name=first_name, last_name=last_name)
user_id = user_obj.id

temp_csv_log_list.append(str(user_id))
Expand All @@ -43,10 +45,10 @@
auth['member_of'] = [auth_gst_id]
auth['group_type'] = u"PUBLIC"
auth['edit_policy'] = u"NON_EDITABLE"
auth['created_by'] = user_id
auth['modified_by'] = user_id
auth['contributors'] = [user_id]
auth['group_admin'] = [user_id]
auth['created_by'] = int(user_id)
auth['modified_by'] = int(user_id)
auth['contributors'] = [int(user_id)]
auth['group_admin'] = [int(user_id)]
auth['agency_type'] = "Student"
auth['_id'] = ObjectId()
auth.save(groupid=auth['_id'])
Expand Down
3 changes: 3 additions & 0 deletions doc/deployer/logout_all_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from gnowsys_ndf.ndf.models import ActiveUsers

ActiveUsers.logout_all_users()
48 changes: 46 additions & 2 deletions gnowsys-ndf/gnowsys_ndf/ndf/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,10 @@ def type_of_names_list(self, smallcase=False):

@staticmethod
def get_name_id_from_type(node_name_or_id, node_type, get_obj=False):
'''
e.g:
Node.get_name_id_from_type('pink-bunny', 'Author')
'''
if not get_obj:
# if cached result exists return it

Expand Down Expand Up @@ -3194,10 +3198,10 @@ def get_active_id_session_keys():
for session in sessions:
data = session.get_decoded()
user_id = data.get('_auth_user_id', 0)
# if user_id:
if user_id:
userid_session_key_dict[user_id] = session.session_key
# uid_list_append(user_id)
# session_key_list.append(session.session_key)
userid_session_key_dict[user_id] = session.session_key

return userid_session_key_dict

Expand All @@ -3207,6 +3211,45 @@ def get_active_id_session_keys():
# else:
# return User.objects.filter(id__in=uid_list)

@staticmethod
def logout_all_users():
"""
Read all available users and all available not expired sessions. Then
logout from each session. This method also releases all buddies with each user session.
"""
from django.utils.importlib import import_module
from django.conf import settings
from django.contrib.auth import logout

request = HttpRequest()

# sessions = Session.objects.filter(expire_date__gte=timezone.now())
sessions = Session.objects.filter(expire_date__gte=timezone.now()).distinct('session_data')

# Experimental trial (aggregate query):
# unique_sessions_list = Session.objects.filter(expire_date__gte=timezone.now()).values('session_data').annotate(Count('session_data')).filter(session_data__count__lte=1)

print('Found %d non-expired session(s).' % len(sessions))

for session in sessions:
try:
user_id = session.get_decoded().get('_auth_user_id')
engine = import_module(settings.SESSION_ENGINE)
request.session = engine.SessionStore(session.session_key)

request.user = User.objects.get(id=user_id)
print ('\nProcessing session of [ %d : "%s" ]\n' % (request.user.id, request.user.username))

logout(request)
print('- Successfully logout user with id: %r ' % user_id)

except Exception as e:
# print "Exception: ", e
pass

Buddy.sitewide_remove_all_buddies()



# class DjangoActiveUsersGroup(object):
# """docstring for DjangoActiveUsersGroup"""
Expand Down Expand Up @@ -3805,6 +3848,7 @@ def remove_all_buddies(self):
active_buddy_authid_list = self.get_active_authid_list_from_single_buddy()

for each_buddy_authid in active_buddy_authid_list:
print "- Released Buddy: ", Node.get_name_id_from_type(each_buddy_authid, u'Author')[0]
self.get_latest_in_out_dict(self.buddy_in_out[each_buddy_authid])['out'] = datetime.datetime.now()

return self
Expand Down
17 changes: 11 additions & 6 deletions gnowsys-ndf/gnowsys_ndf/ndf/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@ def logged_out(sender, user, request, **kwargs):
# print "userid", request.user.id
# print "session val: ", request.session.get('buddies_authid_list', [])

user_id = request.user.id
if user_id and request.session.session_key:
buddy_obj = Buddy.query_buddy_obj(loggedin_userid=user_id,
session_key=request.session.session_key)

if buddy_obj:
buddy_obj.end_buddy_session()
try:
user_id = request.user.id
if user_id and request.session.session_key:
buddy_obj = Buddy.query_buddy_obj(loggedin_userid=user_id,
session_key=request.session.session_key)

if buddy_obj:
buddy_obj.end_buddy_session()
except Exception as e:
print e
pass

# DjangoActiveUsersGroup.removefrom_user_set(user_id)

Expand Down