Exemplo n.º 1
0
 private static User InitUser(User User)
 {
     if (User == null)
         return User;
     var rd = RecordingDirectory.Load(User.DefaultRecordingDirectoryOid);
     if(rd != null)
         User.DefaultRecordingDirectoryDirectoryId = rd.RecordingDirectoryId;
     return User;
 }
Exemplo n.º 2
0
        public User Save()
        {
            var db = DbHelper.GetDatabase();

            bool exists = db.ExecuteScalar<int>("select count(oid) from [user] where (lower(username) = @0 or lower(emailaddress)=@1) and (oid <> @2 or @2 = @3)", this.Username.ToLower(), this.EmailAddress.ToLower(), this.Oid, Globals.SHARED_USER_OID) > 0;
            if (exists)
                throw new Exception("User with that username or email address already exists.");

            db.BeginTransaction();
            try
            {
                User user = new User(){
                    Administrator = this.Administrator,
                    EmailAddress = this.EmailAddress,
                    Oid = this.Oid,
                    Username = this.Username,
                    UserRole = this.UserRole
                };

                if (user.Administrator) // admins should have access to all
                    user.UserRole = Globals.USER_ROLE_ALL;

                if (this.Oid == 0)
                {
                    throw new Exception("User does not exist.");
                }
                else
                {
                    // update existing
                    db.Update("user", "oid", user, user.Oid, new string[] { "EmailAddress", "Administrator", "UserRole" });
                }
                db.CompleteTransaction();

                return User.GetByEmailAddress(this.EmailAddress);
            }
            catch (Exception ex)
            {
                db.AbortTransaction();
                throw ex;
            }
        }
Exemplo n.º 3
0
        public static User CreateUser(string Username, string EmailAddress, string Password, UserRole UserRole, bool Administrator = false, DateTime? LastLoggedInUtc = null)
        {
            var db = DbHelper.GetDatabase();
            db.BeginTransaction();
            try
            {
                User user = new User();
                user.Username = Username;
                user.EmailAddress = EmailAddress;
                if(LastLoggedInUtc == null)
                    LastLoggedInUtc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                user.LastLoggedInUtc = LastLoggedInUtc.Value;
                user.Password = Password;
                user.Administrator = Administrator;
                user.UserRole = UserRole;
                user.DateCreatedUtc = DateTime.UtcNow;
                if (db.Insert("user", "oid", true, user) == null)
                    throw new Exception("Failed to create user.");

                // insert default channels.
                db.Execute("insert into [userchannel](useroid, channeloid,[enabled],number) select @0, oid, [enabled], number from channel", user.Oid);

                // insert default groups
                var channelGroups = ChannelGroup.LoadAll(Globals.SHARED_USER_OID);
                foreach(var cg in channelGroups)
                {
                    ChannelGroup cgNew = new ChannelGroup();
                    cgNew.UserOid = user.Oid;
                    cgNew.ParentOid = cg.Oid;
                    cgNew.Enabled = true;
                    cgNew.Name = "";
                    cgNew.OrderOid = cg.OrderOid;

                    db.Insert("channelgroup", "oid", true, cgNew);
                }

                // create default recording directory
                RecordingDirectory dir = new RecordingDirectory() { UserOid = user.Oid, Username = user.Username, IsDefault = true, Name = "Default", Path = "" };
                db.Insert("recordingdirectory", "oid", true, dir);

                user.DefaultRecordingDirectoryOid = dir.Oid;
                db.Save(user);

                db.CompleteTransaction();

                return user;
            }
            catch (Exception ex)
            {
                Logger.Log("Failed to create user: " + ex.Message + Environment.NewLine + ex.StackTrace);
                db.AbortTransaction();
                return null;
            }
        }