Advertisement
Guest User

Untitled

a guest
Feb 10th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace MVVM3.Model {
  9.     public class User : ValidationBase {
  10.         public static User ActiveUser = null;
  11.  
  12.         private string _username;
  13.         private string _password;
  14.  
  15.         public string Username {
  16.             get => _username;
  17.             set {
  18.                 _username = value;
  19.                 OnPropertyChanged("Username");
  20.             }
  21.         }
  22.         public string Password {
  23.             get => _password;
  24.             set {
  25.                 _password = value;
  26.                 OnPropertyChanged("Password");
  27.             }
  28.         }
  29.  
  30.         public bool ExistsUserWithSameUsername() {
  31.             if (Database.Instance().UserExists(_username)) {
  32.                 return true;
  33.             }
  34.             return false;
  35.         }
  36.  
  37.         public bool CheckPassword(string passw) {
  38.             if (_password == passw) {
  39.                 return true;
  40.             } else {
  41.                 DisplayErrorPassword("Šifra nije ispravna");
  42.                 return false;
  43.             }
  44.         }
  45.  
  46.         public User Clone() {
  47.             User newUser = new User() { Username = _username, Password = _password };
  48.             return newUser;
  49.         }
  50.  
  51.         public void DisplayErrorUsername(string errorMsg) {
  52.             this.ValidationErrors["Username"] = errorMsg;
  53.             this.OnPropertyChanged("IsValid");
  54.             this.OnPropertyChanged("ValidationErrors");
  55.         }
  56.  
  57.         public void DisplayErrorPassword(string errorMsg) {
  58.             this.ValidationErrors["Password"] = errorMsg;
  59.             this.OnPropertyChanged("IsValid");
  60.             this.OnPropertyChanged("ValidationErrors");
  61.         }
  62.  
  63.         protected override void ValidateSelf() {
  64.             if (string.IsNullOrWhiteSpace(this._username)) {
  65.                 this.ValidationErrors["Username"] = "Moraš uneti korisničko ime";
  66.             } else if (Regex.IsMatch(this._username, @"^\d+")) {
  67.                 this.ValidationErrors["Username"] = "Korisničko ime ne može početi brojem";
  68.             }
  69.  
  70.             if (string.IsNullOrWhiteSpace(this._password)) {
  71.                 this.ValidationErrors["Password"] = "Moraš uneti šifru";
  72.             } else if (this._password.Length < Config.minPasswordLength) {
  73.                 this.ValidationErrors["Password"] = "Šifra mora imati minimalno "+ Config.minPasswordLength + " znaka";
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement