Skip to content

Conversation

@tomneedham
Copy link
Contributor

Description

Allows admins to retrieve a parsable list of users that have been seen at least once, but have been inactive for a certain amount of days (no login since then). Scriptable because json output is supported with --output-json.

Usage:
./occ user:inactive 30 for all users who have no logged in for 30 days.

Motivation and Context

Admins wanting reports on inactive users in their instances.

How Has This Been Tested?

  • Created several users in the web UI
  • Ran the command and checked none of these users showed up
  • Altered the last_login times in the accounts table
  • Ran command and checked they appeared correctly

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@tomneedham tomneedham added this to the 10.0.3 milestone Jun 20, 2017
@tomneedham tomneedham self-assigned this Jun 20, 2017
@tomneedham tomneedham requested review from PVince81 and butonic June 20, 2017 14:18
protected function execute(InputInterface $input, OutputInterface $output) {
$days = $input->getArgument('days');
if($days < 1) {
throw new InvalidArgumentException('Days must be above zero');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also restrict to integers ?

}

$inactive = [];
$this->userManager->callForSeenUsers(function(IUser $user) use (&$inactive, $days) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sad that we can't just do a SQL query to retrieve those. The good thing at least is that the user manager now only operated on oc_accounts, not on remote systems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, since we're looking for oc_accounts where last_login != NULL, but I thought it was best to use the usermanager methods for this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. I think this approach is fine if we want it backportable.

$this->userManager->callForSeenUsers(function(IUser $user) use (&$inactive, $days) {
$lastLogin = $user->getLastLogin();
// Difference between now and last login, into days, and rounded down
$daysSinceLastLogin = floor((time() - $lastLogin) / (60*60*24));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For technical accuracy, I'd store time() in a variable before the loop. Imagine that if this loop takes several hours due to millions of entries, the value of time() will advance as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

->willReturn(Base::OUTPUT_FORMAT_JSON);

$this->userManager->expects($this->once())
->method('callForSeenUsers');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a test that actually runs the function in the loop and checks the response format.

You should be able to use ->will($this->returnCallback(function($func) { ... }) and then call the function from within your callback with active and inactive users.

Copy link
Contributor

@PVince81 PVince81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general, a few more minor tweaks would be nice.

Please add unit test for the callback.

@DeepDiver1975
Copy link
Member

isnt this something we can handle within the report command?

@tomneedham
Copy link
Contributor Author

could do, but i read the description of the report command:

shows how many users have access

and that didnt sound too similar to:

reports users who are known to owncloud, but have not logged in for a certain number of days

so I made a new command :)

@PVince81
Copy link
Contributor

PVince81 commented Jul 4, 2017

Explanation makes sense 👍

@PVince81 PVince81 merged commit e146348 into master Jul 4, 2017
@PVince81 PVince81 deleted the inactive-users branch July 4, 2017 08:56
@PVince81
Copy link
Contributor

PVince81 commented Jul 4, 2017

@tomneedham please backport to stable10, looks safe enough

@lock
Copy link

lock bot commented Aug 3, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Aug 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants