public virtual bool Authorize(PermissionRecord permission, User user)
        {
            if (permission == null)
                return false;

            if (user == null)
                return false;
            return Authorize(permission.SystemName, user);
        }
        public virtual bool Authorize(string permissionRecordSystemName, User user)
        {
            if (String.IsNullOrEmpty(permissionRecordSystemName))
                return false;

            var userRole = user.Role;
            if (userRole.Active)
            {
                if (Authorize(permissionRecordSystemName, userRole))
                    //yes, we have such permission
                    return true;
            }
            //no permission found
            return false;
        }
        public JsonResult CreateOrUpdateUser(RegisterModel model)
        {
            try
            {
                var id = Convert.ToInt64(model.Id);

                //update the user
                if (id != 0)
                {
                    var user = _unitOfWork.UserRepository.GetSingle(x => x.Id.Equals(id) && !x.Deleted);
                    if (user != null)
                    {
                        user.FirstName = model.FirstName;
                        user.LastName = model.LastName;
                        user.Phone = model.Phone;
                        user.Cell = model.Cell;
                        user.Username = model.UserName.Trim();
                        user.Password = model.Password;
                        user.Email = model.Email.Trim();
                        user.Address = model.Address;
                        user.City = model.City.Trim();
                        user.ZipCode = model.Zip.ToString();
                        user.StateId = model.StateId;
                        user.RoleId = Convert.ToInt32(model.RoleId);
                        user.Active = model.Active;
                        user.LastUpdatedOn = DateTime.Now;
                        user.LastUpdatedByUserId = MembershipContext.Current.User.Id;
                        user.UserGuid = new Guid(model.GUID);

                        _unitOfWork.UserRepository.Update(user);

                        //DebugChangeTracker(id, _unitOfWork, "UpdateUser", "User");

                        _unitOfWork.Save();
                        return Json(new { success = true, message = "User updated successfully." });
                    }
                }
                else
                {
                    // new user
                    var newUserAvailable = _unitOfWork.UserRepository.Get(x => x.Username.Equals(model.UserName.Trim()));
                    if (newUserAvailable.Count > 0)
                    {
                        return Json(new { success = false, message = "User name already exist. Please try another one." });
                    }
                    var newUser = new User
                    {
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        Phone = model.Phone,
                        Cell = model.Cell,
                        Username = model.UserName.Trim(),
                        Email = model.Email.Trim(),
                        Password = model.Password,
                        Address = model.Address,
                        City = model.City.Trim(),
                        ZipCode = model.Zip.ToString(),
                        StateId = model.StateId,
                        RoleId = Convert.ToInt32(model.RoleId),
                        Active = model.Active,
                        UserGuid = Guid.NewGuid(),
                        CreatedOn = DateTime.Now,
                        LastUpdatedOn = DateTime.Now,
                        CreatedByUserId = MembershipContext.Current.User.Id,
                        LastUpdatedByUserId = MembershipContext.Current.User.Id
                    };
                    _unitOfWork.UserRepository.Insert(newUser);
                    _unitOfWork.Save();

                    return Json(new { success = true, message = "User created successfully." });
                }
            }
            catch (Exception exception)
            {
                Logger.LogException(exception);
                return Json(new { success = false, message = exception.Message });
            }
            return null;
        }