RocketChat/Rocket.Chat

View on GitHub
apps/meteor/app/lib/server/functions/saveUser.js

Summary

Maintainability
F
5 days
Test Coverage

File saveUser.js has 368 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import { Apps, AppEvents } from '@rocket.chat/apps';
import { isUserFederated } from '@rocket.chat/core-typings';
import { Users } from '@rocket.chat/models';
import Gravatar from 'gravatar';
import { Accounts } from 'meteor/accounts-base';
Severity: Minor
Found in apps/meteor/app/lib/server/functions/saveUser.js - About 4 hrs to fix

    Function saveUser has 88 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    export const saveUser = async function (userId, userData) {
        const oldUserData = userData._id && (await Users.findOneById(userData._id));
        if (oldUserData && isUserFederated(oldUserData)) {
            throw new Meteor.Error('Edit_Federated_User_Not_Allowed', 'Not possible to edit a federated user');
        }
    Severity: Major
    Found in apps/meteor/app/lib/server/functions/saveUser.js - About 3 hrs to fix

      Function validateUserData has 70 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      async function validateUserData(userId, userData) {
          const existingRoles = _.pluck(await getRoles(), '_id');
      
          if (userData._id && userId !== userData._id && !(await hasPermissionAsync(userId, 'edit-other-user-info'))) {
              throw new Meteor.Error('error-action-not-allowed', 'Editing user is not allowed', {
      Severity: Major
      Found in apps/meteor/app/lib/server/functions/saveUser.js - About 2 hrs to fix

        Function saveUser has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
        Open

        export const saveUser = async function (userId, userData) {
            const oldUserData = userData._id && (await Users.findOneById(userData._id));
            if (oldUserData && isUserFederated(oldUserData)) {
                throw new Meteor.Error('Edit_Federated_User_Not_Allowed', 'Not possible to edit a federated user');
            }
        Severity: Minor
        Found in apps/meteor/app/lib/server/functions/saveUser.js - About 2 hrs to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function validateUserEditing has 66 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        export async function validateUserEditing(userId, userData) {
            const editingMyself = userData._id && userId === userData._id;
        
            const canEditOtherUserInfo = await hasPermissionAsync(userId, 'edit-other-user-info');
            const canEditOtherUserPassword = await hasPermissionAsync(userId, 'edit-other-user-password');
        Severity: Major
        Found in apps/meteor/app/lib/server/functions/saveUser.js - About 2 hrs to fix

          Function validateUserData has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
          Open

          async function validateUserData(userId, userData) {
              const existingRoles = _.pluck(await getRoles(), '_id');
          
              if (userData._id && userId !== userData._id && !(await hasPermissionAsync(userId, 'edit-other-user-info'))) {
                  throw new Meteor.Error('error-action-not-allowed', 'Editing user is not allowed', {
          Severity: Minor
          Found in apps/meteor/app/lib/server/functions/saveUser.js - About 2 hrs to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Function validateUserEditing has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
          Open

          export async function validateUserEditing(userId, userData) {
              const editingMyself = userData._id && userId === userData._id;
          
              const canEditOtherUserInfo = await hasPermissionAsync(userId, 'edit-other-user-info');
              const canEditOtherUserPassword = await hasPermissionAsync(userId, 'edit-other-user-password');
          Severity: Minor
          Found in apps/meteor/app/lib/server/functions/saveUser.js - About 2 hrs to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Function saveNewUser has 49 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          const saveNewUser = async function (userData, sendPassword) {
              await validateEmailDomain(userData.email);
          
              const roles = (!!userData.roles && userData.roles.length > 0 && userData.roles) || getNewUserRoles();
              const isGuest = roles && roles.length === 1 && roles.includes('guest');
          Severity: Minor
          Found in apps/meteor/app/lib/server/functions/saveUser.js - About 1 hr to fix

            Function saveNewUser has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
            Open

            const saveNewUser = async function (userData, sendPassword) {
                await validateEmailDomain(userData.email);
            
                const roles = (!!userData.roles && userData.roles.length > 0 && userData.roles) || getNewUserRoles();
                const isGuest = roles && roles.length === 1 && roles.includes('guest');
            Severity: Minor
            Found in apps/meteor/app/lib/server/functions/saveUser.js - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Function handleBio has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
            Open

            const handleBio = (updateUser, bio) => {
                if (bio && bio.trim()) {
                    if (bio.length > MAX_BIO_LENGTH) {
                        throw new Meteor.Error('error-bio-size-exceeded', `Bio size exceeds ${MAX_BIO_LENGTH} characters`, {
                            method: 'saveUserProfile',
            Severity: Minor
            Found in apps/meteor/app/lib/server/functions/saveUser.js - About 25 mins to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Function handleNickname has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
            Open

            const handleNickname = (updateUser, nickname) => {
                if (nickname && nickname.trim()) {
                    if (nickname.length > MAX_NICKNAME_LENGTH) {
                        throw new Meteor.Error('error-nickname-size-exceeded', `Nickname size exceeds ${MAX_NICKNAME_LENGTH} characters`, {
                            method: 'saveUserProfile',
            Severity: Minor
            Found in apps/meteor/app/lib/server/functions/saveUser.js - About 25 mins to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Similar blocks of code found in 2 locations. Consider refactoring.
            Open

            const handleBio = (updateUser, bio) => {
                if (bio && bio.trim()) {
                    if (bio.length > MAX_BIO_LENGTH) {
                        throw new Meteor.Error('error-bio-size-exceeded', `Bio size exceeds ${MAX_BIO_LENGTH} characters`, {
                            method: 'saveUserProfile',
            Severity: Major
            Found in apps/meteor/app/lib/server/functions/saveUser.js and 1 other location - About 5 hrs to fix
            apps/meteor/app/lib/server/functions/saveUser.js on lines 252..265

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 149.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Similar blocks of code found in 2 locations. Consider refactoring.
            Open

            const handleNickname = (updateUser, nickname) => {
                if (nickname && nickname.trim()) {
                    if (nickname.length > MAX_NICKNAME_LENGTH) {
                        throw new Meteor.Error('error-nickname-size-exceeded', `Nickname size exceeds ${MAX_NICKNAME_LENGTH} characters`, {
                            method: 'saveUserProfile',
            Severity: Major
            Found in apps/meteor/app/lib/server/functions/saveUser.js and 1 other location - About 5 hrs to fix
            apps/meteor/app/lib/server/functions/saveUser.js on lines 237..250

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 149.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Similar blocks of code found in 3 locations. Consider refactoring.
            Open

                if (
                    isEditingField(user.statusText, userData.statusText) &&
                    !settings.get('Accounts_AllowUserStatusMessageChange') &&
                    (!canEditOtherUserInfo || editingMyself)
                ) {
            Severity: Major
            Found in apps/meteor/app/lib/server/functions/saveUser.js and 2 other locations - About 1 hr to fix
            apps/meteor/app/lib/server/functions/saveUser.js on lines 184..193
            apps/meteor/app/lib/server/functions/saveUser.js on lines 206..215

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 68.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Similar blocks of code found in 3 locations. Consider refactoring.
            Open

                if (
                    isEditingField(user.name, userData.name) &&
                    !settings.get('Accounts_AllowRealNameChange') &&
                    (!canEditOtherUserInfo || editingMyself)
                ) {
            Severity: Major
            Found in apps/meteor/app/lib/server/functions/saveUser.js and 2 other locations - About 1 hr to fix
            apps/meteor/app/lib/server/functions/saveUser.js on lines 184..193
            apps/meteor/app/lib/server/functions/saveUser.js on lines 195..204

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 68.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Similar blocks of code found in 3 locations. Consider refactoring.
            Open

                if (
                    isEditingField(user.username, userData.username) &&
                    !settings.get('Accounts_AllowUsernameChange') &&
                    (!canEditOtherUserInfo || editingMyself)
                ) {
            Severity: Major
            Found in apps/meteor/app/lib/server/functions/saveUser.js and 2 other locations - About 1 hr to fix
            apps/meteor/app/lib/server/functions/saveUser.js on lines 195..204
            apps/meteor/app/lib/server/functions/saveUser.js on lines 206..215

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 68.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Similar blocks of code found in 2 locations. Consider refactoring.
            Open

                    if (userData.email && !(await checkEmailAvailability(userData.email))) {
                        throw new Meteor.Error('error-field-unavailable', `${_.escape(userData.email)} is already in use :(`, {
                            method: 'insertOrUpdateUser',
                            field: userData.email,
                        });
            Severity: Minor
            Found in apps/meteor/app/lib/server/functions/saveUser.js and 1 other location - About 55 mins to fix
            apps/meteor/app/lib/server/functions/saveUser.js on lines 137..142

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 53.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Similar blocks of code found in 2 locations. Consider refactoring.
            Open

                    if (!(await checkUsernameAvailability(userData.username))) {
                        throw new Meteor.Error('error-field-unavailable', `${_.escape(userData.username)} is already in use :(`, {
                            method: 'insertOrUpdateUser',
                            field: userData.username,
                        });
            Severity: Minor
            Found in apps/meteor/app/lib/server/functions/saveUser.js and 1 other location - About 55 mins to fix
            apps/meteor/app/lib/server/functions/saveUser.js on lines 144..149

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 53.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Similar blocks of code found in 2 locations. Consider refactoring.
            Open

                try {
                    nameValidation = new RegExp(`^${settings.get('UTF8_User_Names_Validation')}$`);
                } catch (e) {
                    nameValidation = new RegExp('^[0-9a-zA-Z-_.]+$');
                }
            Severity: Minor
            Found in apps/meteor/app/lib/server/functions/saveUser.js and 1 other location - About 55 mins to fix
            apps/meteor/server/startup/initialData.js on lines 55..59

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 53.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            There are no issues that match your filters.

            Category
            Status