Beispiel #1
0
        private UserContext DemoUserLogin(User user, UnitOfWork uow, int?schoolYearId = null)
        {
            if (user == null)
            {
                return(null);
            }
            if (!user.District.IsDemoDistrict)
            {
                throw new ChalkableException("This login is allowed to demo district only");
            }

            Guid?developerId = null;
            var  developer   = new DeveloperDataAccess(uow).GetDeveloper(user.District.Id);

            if (developer != null)
            {
                developerId = developer.Id;
            }
            var schoolUser = user.SchoolUsers.First();
            var schoolYear = DemoSchoolYearService.GetDemoSchoolYear();
            int roleId;
            var personId = DemoPersonService.GetPersonDataForLogin(schoolUser.User, out roleId);
            var res      = new UserContext(user, CoreRoles.GetById(roleId), user.District, schoolUser.School, developerId, personId, null, schoolYear)
            {
                Claims = ClaimInfo.Create(DemoUserService.GetDemoClaims())
            };

            return(res);
        }
Beispiel #2
0
        public static bool TryConvertFromString(string s, out UserContext userContext)
        {
            var sl = s.Split('\n');

            var t     = typeof(UserContext);
            var props = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            userContext = new UserContext();
            int i = 0;

            foreach (var propertyInfo in props)
            {
                if (propertyInfo.CanWrite && propertyInfo.CanRead && propertyInfo.GetCustomAttribute <Ignore>() == null)
                {
                    if (sl.Length <= i)
                    {
                        userContext = null;
                        return(false);
                    }
                    if (!string.IsNullOrEmpty(sl[i]))
                    {
                        object v;
                        if (!TryParse(propertyInfo.PropertyType, sl[i], out v))
                        {
                            userContext = null;
                            return(false);
                        }
                        propertyInfo.SetValue(userContext, v);
                    }
                    i++;
                }
            }
            userContext.Role = CoreRoles.GetById(userContext.RoleId);
            return(true);
        }
Beispiel #3
0
        public ReportProcessingTaskData(string str)
        {
            var o = JsonConvert.DeserializeObject <ReportProcessingTaskData>(str);

            DistrictId       = o.DistrictId;
            ReportInputModel = o.ReportInputModel;
            UserContext      = o.UserContext;
            UserContext.Role = CoreRoles.GetById(UserContext.RoleId);
            ContentUrl       = o.ContentUrl;
        }
 protected ShortPersonViewData(Person person)
 {
     Id          = person.Id;
     DisplayName = person.DisplayName();
     FullName    = person.FullName();
     FirstName   = person.FirstName;
     LastName    = person.LastName;
     Gender      = person.Gender;
     SchoolId    = person.SchoolRef;
     if (person.RoleRef > 0)
     {
         Role = RoleViewData.Create(CoreRoles.GetById(person.RoleRef));
     }
 }
        public Notification BuildPrivateMessageNotification(DateTime created, PrivateMessage privateMessage, Person fromPerson, Person toPerson)
        {
            var fromPersonRole = CoreRoles.GetById(fromPerson.RoleRef);
            var otherModel     = new
            {
                NeedsUrlLink = fromPersonRole == CoreRoles.TEACHER_ROLE ||
                               fromPersonRole == CoreRoles.STUDENT_ROLE,
                ShortedMessage = StringTools.BuildShortText(privateMessage.Body, 30),
                MessageSubject = privateMessage.Subject,
                SenderId       = privateMessage.Sender.Id,
                SenderName     = privateMessage.Sender.DisplayName()
            };

            return(BuildNotificationFromTemplate(NotificationTemplateProvider.PRIVATE_MESSAGE_NOTIFICATION,
                                                 NotificationType.Message, toPerson, null, null, null,
                                                 privateMessage, fromPerson, otherModel));
        }
Beispiel #6
0
        public static OAuthUserIdentityInfo CreateFromString(string identity)
        {
            var sl = identity.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            if (sl.Length < 4)
            {
                throw new ChalkableException("Invalid identity parameter. Could not conver to OAuthUserIdentity object");
            }

            var syId = int.Parse(sl[2]);

            return(new OAuthUserIdentityInfo
            {
                UserName = sl[0],
                Role = CoreRoles.GetById(int.Parse(sl[1])),
                SchoolYearId = syId == EMPTY_SCHOOL_YEAR_ID ? (int?)null : syId,
                SessionKey = sl[3]
            });
        }
        public PaginatedList <PrivateMessage> GetMessages(int start, int count, bool?read, PrivateMessageType type, string role, string keyword, bool?classOnly, int?acadYear)
        {
            Trace.Assert(Context.PersonId.HasValue);
            Trace.Assert(Context.SchoolLocalId.HasValue);
            Trace.Assert(Context.SchoolYearId.HasValue);

            DateTime?fromDate = null;
            DateTime?toDate   = null;

            if (acadYear.HasValue)
            {
                var schoolYears = ServiceLocator.SchoolYearService.GetSchoolYearsByAcadYear(acadYear.Value);
                if (schoolYears.Count == 0)
                {
                    return(new PaginatedList <PrivateMessage>(new List <PrivateMessage>(), start / count, count));
                }

                fromDate = schoolYears.Min(x => x.StartDate);
                toDate   = schoolYears.Max(x => x.EndDate);
            }

            PrivateMessageSecurity.EnsureMessgingPermission(Context);
            using (var uow = Read())
            {
                var da       = new PrivateMessageDataAccess(uow);
                var roles    = string.IsNullOrWhiteSpace(role) ? new List <string>() : role.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                var rolesIds = roles.Select(x => CoreRoles.GetByName(x).Id).ToList();
                switch (type)
                {
                case PrivateMessageType.Income:
                    var inMsg = da.GetIncomeMessages(Context.PersonId.Value, rolesIds, keyword, read, start, count, fromDate, toDate);
                    return(new PaginatedList <PrivateMessage>(inMsg.Select(x => x), inMsg.PageIndex, inMsg.PageSize, inMsg.TotalCount));

                case PrivateMessageType.Sent:
                    var sentMsg = da.GetSentMessages(Context.PersonId.Value, rolesIds, keyword, start, count, classOnly, fromDate, toDate);
                    return(new PaginatedList <PrivateMessage>(sentMsg.Select(x => x), sentMsg.PageIndex, sentMsg.PageSize, sentMsg.TotalCount));

                default:
                    throw new ChalkableException(ChlkResources.ERR_PRIVATE_MESSAGE_INVALID_TYPE);
                }
            }
        }
Beispiel #8
0
        private static UserContext CreateUserContext(SchoolUser schoolUser, Data.School.Model.SchoolYear schoolYear = null)
        {
            int roleId;

            int personId = schoolUser.User.IsDemoUser
                ? DemoPersonService.GetPersonDataForLogin(schoolUser.User, out roleId)
                : PersonDataAccess.GetPersonDataForLogin(schoolUser.User.District.ServerUrl,
                                                         schoolUser.DistrictRef, schoolUser.UserRef, out roleId);
            var user   = schoolUser.User;
            var school = schoolUser.School;
            var role   = CoreRoles.GetById(roleId);

            Guid?developerId = null;

            if (schoolUser.User.IsDemoUser)
            {
                var developer = CreateMasterSysAdmin().UserService.GetById(user.District.Id);
                if (developer != null)
                {
                    developerId = developer.Id;
                }
            }
            return(new UserContext(user, role, user.District, school, developerId, personId, null, schoolYear));
        }
Beispiel #9
0
        private UserContext SisUserLogin(User user, UnitOfWork uow, ConnectorLocator iNowConnector = null
                                         , StiConnector.Connectors.Model.User iNowUser             = null, int?schoolYearId = null, string sisRedirectUrl = null)
        {
            if (user == null)
            {
                return(null);
            }
            if (user.SisUserId.HasValue)
            {
                try
                {
                    SaveSisToken(user, uow, ref iNowConnector);
                }
                catch (HttpException)
                {
                    return(null);
                }
                Trace.Assert(user.DistrictRef.HasValue);
                var schoolL = ServiceLocator.SchoolServiceLocator(user.DistrictRef.Value, null);
                Data.School.Model.SchoolYear schoolYear;
                SchoolUser schoolUser;
                var        userAcadSessionsIds = iNowConnector.UsersConnector.GetUserAcadSessionsIds();
                if (userAcadSessionsIds.Length == 0)
                {
                    throw new ChalkableException("Current user does not have access to any of school acadSessions");
                }
                if (!schoolYearId.HasValue && userAcadSessionsIds.Length == 1)
                {
                    schoolYearId = userAcadSessionsIds[0];
                }
                PrepareSchoolData(schoolL, user, schoolYearId, userAcadSessionsIds, out schoolYear, out schoolUser);
                if (!schoolUser.School.IsChalkableEnabled)
                {
                    return(null);
                }
                if (iNowUser == null)
                {
                    iNowUser = iNowConnector.UsersConnector.GetMe();
                }
                int roleId;
                int personId   = PersonDataAccess.GetPersonDataForLogin(user.District.ServerUrl, user.DistrictRef.Value, user.SisUserId.Value, out roleId);
                var claimInfos = ClaimInfo.Create(iNowUser.Claims);
                if (roleId == CoreRoles.TEACHER_ROLE.Id)
                {
                    EnsureTeacherChalkableAccess(claimInfos);
                }
#if DEBUG
                var loginTimeOut = (int?)null;
#else
                var loginTimeOut = schoolL.AppSettingService.GetLoginTimeOut();
#endif

                var res = new UserContext(user, CoreRoles.GetById(roleId), user.District, schoolUser.School, null, personId, loginTimeOut, schoolYear, sisRedirectUrl)
                {
                    Claims        = ClaimInfo.Create(iNowUser.Claims),
                    SisApiVersion = iNowConnector.ApiVersion
                };
                return(res);
            }
            throw new UnknownRoleException();
        }