I just found the query online. I want to figure out the way.
Only an enthusiast would like to solve this(caution)
I would like to know how many anonymous users are online.
The server has no a priori notion which users are "online".
You may artificially define the term "session" and then consider the number of session as representing the number of online users.
If you do this and you use Zope sessions to implement your notion of "session", then you can look at the "session_data_manager" to find out how many sessions are available.
This would be more than enough to me, to know how many sessions are still available. If I could have the list of the ids of every active session, I could
easily build the list I need (by the getSessionDataByKey() method)...
I tried with something like :
but not even "manager" is allowed to execute that.
Even trying an External Method :
def activesessionlist( self):
return self.temp_folder.keys()
does not have any effect, instead :
def sessionperiod( self):
return self.temp_folder.getPeriodSeconds()
works right.
Could you please help me find out the list of active sessions? this is
driving me crazy...
Reference
If you'd like check check how many anonymous users are online, you have to create a session for each user whether its anonymous or authenticated. Once you have created the session. after that you can calculate the number of online user using "session_data_manager"
def activesessionlist(self):
# GET Session Data Manager Object
sdm = self.context.session_data_manager
# Create Session if it's not created
session = sdm.getSessionData(create=True)
# Get Container where all the session object is stroed
container = sdm._getSessionDataContainer()
# Get Total Count of Active Session
active_users = len(container.keys())
return active_users
Related
I'm trying to find inactive members in my GitLab-CE instance via the Gitlab API (v4).
One of the criteria for "(in)activity" is, whether a given user is member of any project or group.
While this information seems to be readily available via the webinterface (Groups and projects tab on the user's overview page in the admin area), I cannot find that information via the API.
The only way i currently found is, to iterate over all projects (resp. groups) and check whether the user is member thereof.
This strikes me as very slow (as there are probably zillions of projects), so I'm looking for a more direct way to query the system for all projects where user is member-of.
As in doc(https://docs.gitlab.com/ce/api/members.html), you can use:
GET /groups/:id/members
GET /projects/:id/members
to get only members added directly in a group/project
or:
GET /groups/:id/members/all
GET /projects/:id/members/all
to get all members (even those inherit from groups above)
---EDIT regards to #Nico question ---
In order to know if a user is a member of a project the solution tested by #umläute is to iterate over project members then all subgroup untill it reaches the user:
Given \fu\bar\project_p
With project_p.id = 1
bar.id = 10
fu.id = 100
Is user 'Nico' a member of project_p ?
GET /projects/1/members returns ('Paul') / No
GET /groups/10/members returns ('Marc', 'Jean') / No
GET /groups/100/members returns ('Nico') / Yes
Instead Gitlab provide an other API :
GET /projects/1/members/all returns ('Paul', 'Marc', 'Jean', 'Nico') / Yes
I want to have the same WordPress users in two different databases
For example, if a user registers on SiteA, then he can login to SiteB. And reverse.
Also i want create same cookie for both after login.
mywebsite.com/ (SiteA_DB)
mywebsite.com/blog/ (SiteB_DB)
I've never done this before and maybe Wordpress has hooks to archive this, but I prefer using mysql for such a trick.
You could try ..
.. using 'federated storage' ( https://stackoverflow.com/a/24532395/10362812 )This is my favorite, because you don't even have to share a database or even the mysql serverThe downside is, that it doesn't work with db cache and uses an additional connection.
.. creating a 'view' ( https://stackoverflow.com/a/1890165/10362812 )This should be possible when using the database-name in the query itself and it would be the simplest solution if it works. Downside: The 2 tables have to share the same mysql-server and have to be assigned to the same user as far as I know.
-- **Backup your database before trying!** --
DROP TABLE `second_database`.`wp_users`;
DROP TABLE `second_database`.`wp_usermeta`;
CREATE VIEW `second_database`.`wp_users` AS SELECT * FROM `first_database`.`wp_users`;
CREATE VIEW `second_database`.`wp_usermeta` AS SELECT * FROM `first_database`.`wp_usermeta`;
This should work, according to: Creating view across different databases
.. creating a 'shadow copy' ( https://stackoverflow.com/a/1890166/10362812 )Works with caching and is a standalone tableDownsides as 2. solution + a bit of setup and I think it might be the worst option in performance
This were answers to this question: How do I create a table alias in MySQL
I merged them together for you and made them fit your use-case.
Please also notice, that solution 1 and 2 will replace your current user-tables auf "second_database" because you write directly into "first_database" when querying the fed. storage or the view. This can lead to problems with user-role plugins. You should take care of syncing the plugin-options too, if you should use one of them and in case it uses different tables or 'wp_options' values.
Let me know if this works, I have to do a similar task next week. While researching I found the linked answers.
EDIT: I was missing the point of "cookie-sharing" in my answer. Your example shows a blog on the same domain - you should be able to change the way wordpress sets its cookies to be domain-wide. What I did once for 2 different domains was, that I hooked into the backend (is_admin) and added a javascript which did a post-request to siteB, receiving a token which is stored but marked as 'invalid' on siteB. This token then was passed back to my plugin on siteA which checked if the user is logged_in and (in my case) have adminrights (current_user_can()) and if so, it was sending this token back to sideB which was marking this token as valid to login. (Make sure only sideA can tell sideB to make this token valid!) Once a user is seen with this token in a cookie on siteB, the user is logged-in automatically in the background. Also I made this bidirectional. I am sorry, that I can't share the code for you. I don't have access to it anymore.
Greetings, Eric!
I am working on sensenet API. I faced an issue with setPermission on sensenetAPI security.
As per concern, when I create a document I would like to give See, open, Save and RunApplication permission as a default for newly created document to the user(User is taken from the function parameter).
To achieve this I use below code
public static void SetCollabUserSecurity(string myUserEmailId, Node myNodetToSetSecurity)
{
var domainName = "Builtin";
var strUsername = GetNameFromEmail(myUserEmailId);
User user;
using (new SystemAccount())
{
user = User.Load(domainName, strUsername);
if (user != null && user.Enabled)
{
var myUser = user;
myNodetToSetSecurity.Security.SetPermission(myUser, true, PermissionType.See,
PermissionValue.Allow);
myNodetToSetSecurity.Security.SetPermission(myUser, true, PermissionType.Open,
PermissionValue.Allow);
myNodetToSetSecurity.Security.SetPermission(myUser, true, PermissionType.Save,
PermissionValue.Allow);
myNodetToSetSecurity.Security.SetPermission(myUser, true, PermissionType.RunApplication,
PermissionValue.Allow);
}
}
}
While I am using this function, my process for creating document becomes time consuming. It takes around 40 second time for execution.
So in case of, if I would like to share the same newly created document with multiple users, lets say there are 3 user and I want to give the above permission to all of them then my single function call takes 120 second (2 minute) time to simply assign permission.
Is there any Odata REST API call available or any sensenet library call available through which I can assign...
1) multiple permission to multiple user for single document or
2) multiple permission to single user for single document
Can anyone help to come out from this issue?
Thanks!
C# api
On the server there is a c# api for managing permissions, please check this article for details. You may use the AclEditor class for setting multiple permissions in one round. Please note that you have to call the Apply method at the end to actually perform the operation.
// set permissions on folder1, folder2 and file1 for two users and a group
SecurityHandler.CreateAclEditor()
.Allow(folder1.Id, user1.Id, false, PermissionType.Open, PermissionType.Custom01)
.Allow(folder2.Id, user2.Id, false, PermissionType.Open)
.Allow(file1.Id, editorsGroup.Id, false, PermissionType.Save)
.Apply();
As a side note: in most cases it is better to work with groups than users when assigning permissions. So it is advisable to give permissions to a group and put users into the group as members instead of assigning permissions to users directly.
Also: it is easier to maintain a simpler security structure, for example if you assign a permission on the parent container (e.g. a folder) instead of on individual files. Of course if you have to set permission per file, then it is fine.
OData api
The same api is available from the client through the REST api. Please take a look at the SetPermissions action in this article or the similar api in the JavaScript client library of sensenet.
I am using the ASP.NET Identity framework, with the EntityFramework provider (IdentityDbContext).
I attempt to create a user by calling UserManager.CreateAsync(..).
It returns an object of type IdentityResult, with the following values:
{
Succeeded: false,
Errors: ["Name AndrewShepherd is already taken."]
}
The error is valid - there is indeed another user called "AndrewShepherd" in the database. This is not a name the user picked; instead I am generating this name from the Outh2 account information provided by their Google account. (Google Accounts don't enforce unique names, just unique email addresses).
Since the problem is a duplicate name, I can simply try appending a number to the name and trying again. I would programmatically attempt to create AndrewShepherd_1, AndrewShepherd_2, AndrewShepherd_3 until I am either successful or get a different error.
But how do I programatically determine that the problem was a duplicate name?
Options are:
Perform a pattern match on the error string. This solution is guaranteed to break when the next version of ASP.NET Identity has a differently worded error messages or we internationalize the website.
Run the check before creating the user. I would call UserStore.FindByNameAsync to determine if the name had already been taken before invoking UserManager.CreateASync(..). This would have a small concurrency risk if two different sessions are attempting to add the same user name at the same time.
It would be some much easier if we could simply perform a check like this:
if(identityResult.Errors.Where(e => e.ErrorCode == IdentityErrors.DuplicateName).Any())
Is there a way I can perform a unique user check then add a user, that's safe in a concurrent environment?
As an alternaive, you can check if the user name is exist in your DB like that. If user name is already taken, you can change new user name like AndrewShepherd_1, AndrewShepherd_2, AndrewShepherd_3.
using (YourProjectName.Models.DefaultConnection db = new Models.DefaultConnection ())
{
if (db.AspNetUsers.Select(q => q.UserName).Contains("usernameThatYouWantToCheck"))
{
/*Your code*/
}
}
I am using ASP.NET MVC 3 to track hits on a set of stored links. I run into problems when displaying the hits' counts. I think this is because since I am using lazy loading, whenever I call
link.Hits.Count
it loads all of each hit's data, including such things as agent and referrer information. (Hits is a Collection.) This is an issue when a link has over 9000 hits. Is there a way of just getting the Hits' Count without it pulling in the Hits' data?
If the Hits has been written like this, the Count will work:
Hits = repository.GetAll(....).Where(....);
But if like this, the Count will not work, because the ToList() has already loaded all the data:
Hits = repository.GetAll(....).Where(....).ToList();