public UserPasswordDialog(User user)
        {
            InitializeComponent();
            this.backgroundWorker = new BackgroundWorker();
            this.backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            this.backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);

            //Initialize
            this.user = user.ID;
            this.UserID.Text = user.ID.ToString();
            this.UserName.Text = user.Name;
            this.UserDescription.Text = user.Description;
            this.UserUsername.Text = user.Username;
        }
 public UserEditDialog(User user)
 {
     InitializeComponent();
     this.backgroundWorker = new BackgroundWorker();
     this.backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
     this.backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
     if (user != null)
         this.user = (User)user.Clone();
     else
     {
         this.user = new User { ID = 0, Username = "", Name = "<New User>", Description = "" };
         modification = false;
     }
     //Initialize
     this.UserID.Text = this.user.ID.ToString();
     this.UserName.Text = this.user.Name;
     this.UserUsername.IsReadOnly = modification;
     this.UserDescription.Text = this.user.Description;
     this.UserUsername.Text = this.user.Username;
     this.UserPermissionAdmin.IsChecked = (this.user.Permission & User.PERMISSION_ADMIN) > 0;
     this.UserPermissionSubscribe.IsChecked = (this.user.Permission & User.PERMISSION_SUBSCRIBE) > 0;
     this.UserPermissionControl.IsChecked = (this.user.Permission & User.PERMISSION_CONTROL) > 0;
     this.UserPermissionStats.IsChecked = (this.user.Permission & User.PERMISSION_STATS) > 0;
 }
Beispiel #3
0
        private void UserUpdatedHandler(User user)
        {
            // update myself
            if (UserIsLoggedIn && user.ID == UserCurrent.ID)
            {
                UserCurrent.Name = user.Name;
                UserCurrent.Description = user.Description;
                UserCurrent.Permission = user.Permission;
            }

            if (!UserHasAdminPermission) return;
            if (users == null) return;
            users_mutex.WaitOne();
            foreach (User u in users)
                if (u.ID == user.ID)
                {
                    u.Description = user.Description;
                    u.Name = user.Name;
                    u.Username = user.Username;
                    u.Permission = user.Permission;
                    int index = users.IndexOf(u);
                    users.Remove(u);
                    users.Insert(index, u);
                    break;
                }
            users_mutex.ReleaseMutex();
        }
Beispiel #4
0
 private void UserCreatedHandler(User user)
 {
     if (!UserHasAdminPermission) return;
     if (users == null) return;
     foreach (User u in users)
         if (u.ID == user.ID) return;
     users_mutex.WaitOne();
     users.Insert(0, user);
     users_mutex.ReleaseMutex();
 }
Beispiel #5
0
 /// <summary>
 /// Called when user is authorized to login, retrieve user info
 /// </summary>
 /// <param name="username">Username</param>
 /// <param name="connection">Database connection</param>
 /// <returns>Detailed user information</returns>
 public static User UserGetByUsername(string username, Connection connection)
 {
     SqlCommand cmd = _setup("r_user_get_by_username", connection);
     _param(cmd, "@username", SqlDbType.VarChar, username);
     User user = null;
     SqlDataReader reader = cmd.ExecuteReader();
     if (reader.Read())
         user = new User
         {
             ID = Convert.ToInt32(reader["id"]),
             Username = reader["r_username"].ToString(),
             Name = reader["r_name"].ToString(),
             Description = reader["r_description"].ToString(),
             Permission = Convert.ToInt32(reader["r_permission"])
         };
     reader.Close();
     reader.Dispose();
     return user;
 }
Beispiel #6
0
 /// <summary>
 /// User Logout
 /// </summary>
 public void UserLogout()
 {
     SubscriptionManager.Instance.UnsubscribeAll(this);
     if (this.user != null)
     {
         ClientEventEnqueue(new ClientEvent { Type = ClientEventType.UserLogout, Timestamp = DateTime.Now });
         this.user = null;
     }
     queue_mutex.WaitOne();
     queue.Clear();
     queue_mutex.ReleaseMutex();
 }
Beispiel #7
0
 /// <summary>
 /// User Login
 /// </summary>
 /// <param name="username">Username</param>
 /// <param name="hashpassword">Hashed Password</param>
 /// <returns></returns>
 public UserLoginResult UserLogin(string username, string hashpassword)
 {
     if (username == null || hashpassword == null || username == "" || hashpassword.Length != Util.Hash.StringLength)
         return UserLoginResult.InvalidArgument;
     if (this.user != null) return UserLoginResult.UserAlreadyLoggedIn;
     if (!Util.Validator.IsUsername(username)) return UserLoginResult.InvalidUsername;
     string password = Data.StoredProcedure.UserPasswordGetByUsername(username.ToLower(), Connection);
     if (password == null || password == "") return UserLoginResult.Mismatch;
     if (Common.EncryptPassword(Common.GetPasswordPrefix(password), hashpassword) == password)
     {
         this.user = Data.StoredProcedure.UserGetByUsername(username, Connection);
         if (UserIsLoggedIn)
             return UserLoginResult.Success;
         else
             return UserLoginResult.Error;
     }
     else
     {
         this.user = null;
         return UserLoginResult.Mismatch;
     }
 }
Beispiel #8
0
 public bool UserUpdate(User user, Guid session)
 {
     if (user == null || !user.IsValid) return false;
     ClientSession _session = ClientSession.Get(session);
     if (_session == null) return false;
     if (!_session.UserIsLoggedIn) return false;
     if (_session.UserHasAdminPermission)
     {
         if (Data.StoredProcedure.UserUpdateById(user.ID, user.Name, user.Description, user.Permission, _session.Connection))
         {
             Clients.ClientSession.Notify(new ClientEvent { Type = ClientEventType.UserUpdate, User = user, Timestamp = DateTime.Now });
             return true;
         }
         else
             return false;
     }
     else if (_session.User.ID == user.ID)
     {
          return _session.UserUpdate(user.Name, user.Description);
     }
     return false;
 }
Beispiel #9
0
 private void SessionCheck()
 {
     if (!service.SessionExists(session))
     {
         session = service.SessionNew();
         user = null;
     }
 }
Beispiel #10
0
 public int UserCreate(User user, Guid session)
 {
     ClientSession _session = ClientSession.Get(session);
     if (_session == null) return -2;
     if (user == null)
         return -3;
     user.ID = Int32.MaxValue;
     if (!user.IsValid)
         return -3;
     if (!_session.UserHasAdminPermission) return -4;
     if (!Util.Validator.IsUsername(user.Username)) return -5;
     int id = Data.StoredProcedure.UserCreate(user.Username, user.Name, user.Description, user.Permission, _session.Connection);
     if (id > 0)
     {
         user.ID = id;
         Clients.ClientSession.Notify(new ClientEvent { Type = ClientEventType.UserCreation, User = user, Timestamp = DateTime.Now });
     }
     return id;
 }
Beispiel #11
0
 protected virtual void Initialize()
 {
     service.Open();
     session = service.SessionNew();
     user = null;
 }
Beispiel #12
0
 public bool UserUpdate(User user)
 {
     if (user == null || !user.IsValid)
         throw new ArgumentException();
     SessionCheck();
     if (!UserHasAdminPermission) throw new PermissionDeniedException();
     return service.UserUpdate(user, session);
 }
Beispiel #13
0
        /// <summary>
        /// User Logout
        /// </summary>
        public virtual void UserLogout()
        {
            SessionCheck();

            if (!UserIsLoggedIn) return;
            service.UserLogout(session);
            //clear cache when user exits
            this.user = null;
        }
Beispiel #14
0
 /// <summary>
 /// User Login
 /// </summary>
 /// <param name="username"></param>
 /// <param name="hashpassword"></param>
 /// <returns></returns>
 public bool UserLogin(string username, string hashpassword)
 {
     if (username == null || hashpassword == null || hashpassword.Length != Util.Hash.StringLength)
         throw new ArgumentException();
     SessionCheck();
     UserLoginResult result = service.UserLogin(username, hashpassword, session);
     switch (result)
     {
         case UserLoginResult.InvalidArgument:
             throw new ArgumentException();
         case UserLoginResult.InvalidSession:
             throw new InvalidSessionException();
         case UserLoginResult.InvalidUsername:
             throw new InvalidUsernameException();
         case UserLoginResult.UserAlreadyLoggedIn:
             throw new UserAlreadyLoggedInException();
         case UserLoginResult.Mismatch:
             return false;
         case UserLoginResult.Success:
             this.user = service.UserCurrent(session);
             return this.user != null;
         default:
             throw new SystemException();
     }
 }
Beispiel #15
0
 /// <summary>
 /// Creates a new user, need to set password later
 /// </summary>
 /// <param name="user"></param>
 /// <returns></returns>
 public bool UserCreate(User user)
 {
     if (user == null) throw new ArgumentException();
     user.ID = Int32.MaxValue;
     if (!user.IsValid) throw new ArgumentException();
     SessionCheck();
     if (!UserHasAdminPermission) throw new PermissionDeniedException();
     int id = service.UserCreate(user, session);
     if (id > 0)
     {
         user.ID = id;
         return true;
     }
     switch (id)
     {
         case -1: throw new DuplicateUsernameException();
         case -2: throw new InvalidSessionException();
         case -3: throw new ArgumentException();
         case -4: throw new PermissionDeniedException();
         case -5: throw new InvalidUsernameException();
     }
     return false;
 }