-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revise model migration. Add user_password_history command. Add management command tests. Improve password tests. Add Django admin functionality for new PasswordExpiry and PasswordHistory models.
- Loading branch information
Showing
8 changed files
with
407 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import datetime | ||
import pytz | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
|
||
from account.models import PasswordHistory | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
help = "Create password history for all users without existing history." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"-d", "--days", | ||
type=int, | ||
nargs="?", | ||
default=10, | ||
help="age of current password (in days)" | ||
) | ||
parser.add_argument( | ||
"-f", "--force", | ||
action="store_true", | ||
help="create new password history for all users, regardless of existing history" | ||
) | ||
|
||
def handle(self, *args, **options): | ||
User = get_user_model() | ||
users = User.objects.all() | ||
if not options["force"]: | ||
users = users.filter(password_history=None) | ||
|
||
if not users: | ||
return "No users found without password history" | ||
|
||
days = options["days"] | ||
timestamp = datetime.datetime.now(tz=pytz.UTC) - datetime.timedelta(days=days) | ||
|
||
# Create new PasswordHistory on `timestamp` | ||
PasswordHistory.objects.bulk_create( | ||
[PasswordHistory(user=user, timestamp=timestamp) for user in users] | ||
) | ||
|
||
return "Password history set to {} for {} users".format(timestamp, len(users)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.