Skip to content

Commit

Permalink
add password protection and fix destroy bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexaorrico committed Dec 23, 2017
1 parent f148313 commit 691c0f1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion console.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def do_destroy(self, arg):
if len(args) > 1:
key = args[0] + "." + args[1]
if key in models.storage.all():
models.storage.all().pop(key)
models.storage.all()[key].delete()
models.storage.save()
else:
print("** no instance found **")
Expand Down
7 changes: 6 additions & 1 deletion models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,20 @@ def save(self):
models.storage.new(self)
models.storage.save()

def to_dict(self):
def to_dict(self, save_to_disk=False):
"""returns a dictionary containing all keys/values of the instance"""
new_dict = self.__dict__.copy()
if "created_at" in new_dict:
new_dict["created_at"] = new_dict["created_at"].isoformat()
if "updated_at" in new_dict:
new_dict["updated_at"] = new_dict["updated_at"].isoformat()
if '_password' in new_dict:
new_dict['password'] = new_dict['_password']
new_dict.pop('_password', None)
new_dict["__class__"] = self.__class__.__name__
new_dict.pop('_sa_instance_state', None)
if not save_to_disk:
new_dict.pop('password', None)
return new_dict

def delete(self):
Expand Down
2 changes: 1 addition & 1 deletion models/engine/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def save(self):
"""serializes __objects to the JSON file (path: __file_path)"""
json_objects = {}
for key in self.__objects:
json_objects[key] = self.__objects[key].to_dict()
json_objects[key] = self.__objects[key].to_dict(save_to_disk=True)
with open(self.__file_path, 'w') as f:
json.dump(json_objects, f)

Expand Down
22 changes: 19 additions & 3 deletions models/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/python3
""" holds class User"""
from base64 import b64encode
import hashlib
import models
from models.base_model import BaseModel, Base
from os import getenv
Expand All @@ -13,8 +15,9 @@ class User(BaseModel, Base):
__tablename__ = 'users'
email = Column(String(128),
nullable=False)
password = Column(String(128),
nullable=False)
_password = Column('password',
String(128),
nullable=False)
first_name = Column(String(128),
nullable=True)
last_name = Column(String(128),
Expand All @@ -27,10 +30,23 @@ class User(BaseModel, Base):
cascade="all, delete-orphan")
else:
email = ""
password = ""
_password = ""
first_name = ""
last_name = ""

def __init__(self, *args, **kwargs):
"""initializes user"""
if 'password' in kwargs:
pwd = hashlib.md5(b64encode(kwargs['password'].encode('ascii')))
kwargs['password'] = pwd.hexdigest()
super().__init__(*args, **kwargs)

@property
def password(self):
return self._password

@password.setter
def password(self, pwd):
"""hashing password values"""
self._password = hashlib.md5(b64encode(pwd
.encode('ascii'))).hexdigest()

0 comments on commit 691c0f1

Please sign in to comment.